
using System;
using System.Collections.Generic;
using System.Data;
using CapeOpen;

namespace PVTLibrary
{

    public enum Phase
    {
        Liquid,
        Liquid1,
        Liquid2,
        Liquid3,
        Aqueous,
        Vapor,
        Solid,
        Mixture
    };

    public enum State
    {
        Liquid = 0,
        Vapor = 1,
        Solid = 2
    }

    public enum FlashSpec
    {
        P,
        T,
        S,
        H,
        V,
        U,
        VAP
    };

    public enum ThermoProperty
    {
        ActivityCoefficient,
        Fugacity,
        FugacityCoefficient
    }

    public enum PackageType
    {
        EOS = 0,
        ActivityCoefficient = 1,
        ChaoSeader = 2,
        VaporPressure = 3,
        Miscelaneous = 4,
        CorrespondingStates = 5,
        CAPEOPEN = 6
    }

    public enum FlashMethod
    {
        DWSIMDefault = 0,
        InsideOut = 1,
        GlobalSetting = 2,
        InsideOut3P = 3,
        GibbsMin2P = 4,
        GibbsMin3P = 5,
        NestedLoops3P = 6,
        NestedLoopsSLE = 7,
        NestedLoopsImmiscible = 8,
        SimpleLLE = 9,
        NestedLoopsSLE_SS = 10,
        NestedLoops3PV2 = 11
    }

    public enum Parameter
    {
        PHFlash_Internal_Loop_Tolerance = 0,
        PSFlash_Internal_Loop_Tolerance = 1,
        PHFlash_External_Loop_Tolerance = 2,
        PSFlash_External_Loop_Tolerance = 3,
        PHFlash_Maximum_Number_Of_External_Iterations = 4,
        PSFlash_Maximum_Number_Of_External_Iterations = 5,
        PHFlash_Maximum_Number_Of_Internal_Iterations = 6,
        PSFlash_Maximum_Number_Of_Internal_Iterations = 7,
        PTFlash_Maximum_Number_Of_External_Iterations = 8,
        PTFlash_Maximum_Number_Of_Internal_Iterations = 9,
        PTFlash_External_Loop_Tolerance = 10,
        PTFlash_Internal_Loop_Tolerance = 11
    }


    /// <summary>
    /// The Property Package Class contains methods to do thermodynamic calculations for all supported phases in DTL.
    /// The base class is inherited by each implemented property package, which contains its own methods
    /// </summary>
    [Serializable]
    public abstract class PropertyPackage
        //CAPE-OPEN 1.0 Interfaces
        //: ICloneable, ICapeIdentification, ICapeThermoPropertyPackage, ICapeUtilities, ICapeThermoEquilibriumServer, ICapeThermoCalculationRoutine
        //'CAPE-OPEN 1.1 Interfaces
        //,ICapeThermoPhases, ICapeThermoPropertyRoutine, ICapeThermoCompounds, ICapeThermoUniversalConstant
        //,ICapeThermoMaterialContext, ICapeThermoEquilibriumRoutine
        //'CAPE-OPEN Error Interfaces
        //,ECapeUser, ECapeUnknown, ECapeRoot
        //'IDisposable
        : IDisposable
    {

        public const string ClassId = "";

        private PROPS m_props = new PROPS();

        private MaterialStream m_ms = null;

        private List<string> m_ss = new List<string>();
        private bool m_configurable = false;

        public Dictionary<string, double> m_par = null;

        private string _tag = "";
        private string _uniqueID = "";

        private DataTable m_ip;

        private FlashMethod _flashalgorithm;

        public PackageType _packagetype ;

        public BostonFournierInsideOut3P _brio3 = new BostonFournierInsideOut3P();
        public BostonBrittInsideOut _bbio = new BostonBrittInsideOut();
        public DWSIMDefault _dwdf = new DWSIMDefault();
        public NestedLoops3P _nl3 = new NestedLoops3P();
        public NestedLoops3PV2 _nl3v2 = new NestedLoops3PV2();
        public NestedLoopsSLE _nlsle = new NestedLoopsSLE();
        public NestedLoopsImmiscible _nli = new NestedLoopsImmiscible();
        public GibbsMinimization3P _gm3 = new GibbsMinimization3P();

        public bool _ioquick = true;
        public int _tpseverity = 0;
        public string[] _tpcompids = {};

        public Dictionary<string, PhaseInfo> _phasemappings = new Dictionary<string, PhaseInfo>();

        private double LoopVarF;
        private double LoopVarX ;
        private State LoopVarState;

        [NonSerialized] 
        private object _como; //CAPE-OPEN Material Object


        public PropertyPackage()
        {
            Initialize();
        }

        public PropertyPackage(bool capeopenmode)
        {
            //modif adel
            //My.Application.CAPEOPENMode = capeopenmode

            if(capeopenmode)
            {
                //'initialize collections
                _selectedcomps = new Dictionary<string, ConstantProperties>();
                _availablecomps = new Dictionary<string, ConstantProperties>();
            }
            Initialize();
        }

        void ConfigParameters()
        {
            m_par = new Dictionary<string, double>();

            this.Parameters.Clear();
            this.Parameters.Add("PP_PHFILT", 0.001);
            this.Parameters.Add("PP_PSFILT", 0.001);
            this.Parameters.Add("PP_PHFELT", 0.001);
            this.Parameters.Add("PP_PSFELT", 0.001);
            this.Parameters.Add("PP_PHFMEI", 50);
            this.Parameters.Add("PP_PSFMEI", 50);
            this.Parameters.Add("PP_PHFMII", 100);
            this.Parameters.Add("PP_PSFMII", 100);
            this.Parameters.Add("PP_PTFMEI", 100);
            this.Parameters.Add("PP_PTFMII", 100);
            this.Parameters.Add("PP_PTFILT", 0.001);
            this.Parameters.Add("PP_PTFELT", 0.001);
            this.Parameters.Add("PP_RIG_BUB_DEW_FLASH_INIT", 0);
            this.Parameters.Add("PP_IDEAL_MIXRULE_LIQDENS", 0);
            this.Parameters.Add("PP_FLASHALGORITHM", 2);
            this.Parameters.Add("PP_USEEXPLIQDENS", 0);
            this.Parameters.Add("PP_USEEXPLIQTHERMALCOND", 1);
        }

        /// <summary>
        /// Globally sets a value for the maximum number of iteractions and tolerances for the flash algorithms.
        /// </summary>
        /// <param name="p">Parameter to be set.</param>
        /// <param name="value">Value of the parameter.</param>
        /// <remarks></remarks>
        public void SetParameterValue(Parameter p , object value )
        {
           switch(p)
           {
               case Parameter.PHFlash_External_Loop_Tolerance:
                   this.Parameters["PP_PHFELT"] = value;                   
                   break;

               case Parameter.PHFlash_Internal_Loop_Tolerance:
                   this.Parameters("PP_PHFILT") = value;
                   break;

               case Parameter.PHFlash_Maximum_Number_Of_External_Iterations:
                   this.Parameters("PP_PHFMEI") = value;
                   break;

               case Parameter.PHFlash_Maximum_Number_Of_Internal_Iterations:
                   this.Parameters("PP_PHFMII") = value;
                   break;

               case Parameter.PSFlash_External_Loop_Tolerance:
                   this.Parameters("PP_PSFELT") = value;
                   break;

                case Parameter.PSFlash_Internal_Loop_Tolerance:
                   this.Parameters("PP_PSFILT") = value;
                   break;;

                case Parameter.PSFlash_Maximum_Number_Of_External_Iterations:
                   this.Parameters("PP_PSFMEI") = value;
                   break;

                case Parameter.PSFlash_Maximum_Number_Of_Internal_Iterations:
                   this.Parameters("PP_PSFMII") = value;
                   break;

                case Parameter.PTFlash_External_Loop_Tolerance:
                   this.Parameters("PP_PTFELT") = value;
                   break;

                case Parameter.PTFlash_Internal_Loop_Tolerance:
                   this.Parameters("PP_PTFILT") = value;
                   break;

                case Parameter.PTFlash_Maximum_Number_Of_External_Iterations:
                   this.Parameters("PP_PTFMEI") = value;
                   break;

                case Parameter.PTFlash_Maximum_Number_Of_Internal_Iterations:
                   this.Parameters("PP_PTFMII") = value;
                    break;;

           }
        }

        public bool AddCompound(string compname ) 
        {

            if(!_selectedcomps.ContainsKey(compname))
            {
                var tmpcomp = new ConstantProperties();
                tmpcomp = _availablecomps(compname);
                _selectedcomps.Add(tmpcomp.Name, tmpcomp);
                _availablecomps.Remove(tmpcomp.Name);
                return true;
            }
            else
            {
                return false;
            }
        }

        public void CreatePhaseMappings()
        {
            _phasemappings = new Dictionary<string, PhaseInfo>();

            _phasemappings.Add("Vapor", new PhaseInfo("", 2, Fase.Vapor));
            _phasemappings.Add("Liquid1", new PhaseInfo("", 3, Fase.Liquid1));
            _phasemappings.Add("Liquid2", new PhaseInfo("", 4, Fase.Liquid2));
            _phasemappings.Add("Liquid3", new PhaseInfo("", 5, Fase.Liquid3));
            _phasemappings.Add("Aqueous", new PhaseInfo("", 6, Fase.Aqueous));
            _phasemappings.Add("Solid", new PhaseInfo("", 7, Fase.Solid));
        }

        public void CreatePhaseMappingsDW()
        {
            _phasemappings = new Dictionary<string, PhaseInfo>();
            _phasemappings.Add("Vapor",   new PhaseInfo("Vapor", 2, Fase.Vapor));
            _phasemappings.Add("Liquid1", new PhaseInfo("Liquid", 3, Fase.Liquid1));
            _phasemappings.Add("Liquid2", new PhaseInfo("Liquid2", 4, Fase.Liquid2));
            _phasemappings.Add("Solid",   new PhaseInfo("Solid", 7, Fase.Solid));
        }


        protected  Dictionary<string, ConstantProperties> _availablecomps =null;
        protected  Dictionary<string, ConstantProperties> _selectedcomps =null;

        public  Dictionary<string, ConstantProperties> AvailableCompounds
        {
            get{
                return _availablecomps;
            }
        }

        public  Dictionary<string, ConstantProperties> SelectedCompounds
        {
            get{
                return _selectedcomps;
            }
        }



        /// <summary>
        /// Get or sets the list of compounds to be used in the liquid phase stability test during three-phase flash calculations.
        /// </summary>
        /// <value>A string array containing the names of the compounds.</value>
        /// <returns></returns>
        /// <remarks></remarks>
        public string[] StabilityTestKeyCompounds 
        {
            get
            {
                return _tpcompids;
            }
            set
            {

                _tpcompids = value;
            }
        }

        /// <summary>
        /// Defines the severity of the liquid phase stability test during three-phase flash calculations.
        /// </summary>
        /// <value>0 is the lowest, 2 is the highest.</value>
        /// <returns></returns>
        /// <remarks></remarks>
        public int StabilityTestSeverity
        {
            get
            {
                return _tpseverity;
            }
            set
            {
                _tpseverity = value;
            }
        }




        public  Dictionary<string, PhaseInfo>  PhaseMappings
        {
            get
            {
                if (_phasemappings == null)
                    CreatePhaseMappingsDW();
                else if (_phasemappings.Count == 0)
                    CreatePhaseMappingsDW();
                return _phasemappings;
            }
        }

        /// <summary>
        /// Returns the flash algorithm selected for this property package.
        /// </summary>
        /// <value></value>
        /// <returns>A FlashMethod value with information about the selected flash algorithm.</returns>
        /// <remarks></remarks>
        /// 
        public FlashMethod FlashAlgorithm
        {
            get
            {
                return _flashalgorithm;
            }
            set
            {
                _flashalgorithm = value;
            }
        }


        /// <summary>
        /// Returns the FlashAlgorithm object instance for this property package.
        /// </summary>
        /// <value></value>
        /// <returns>A FlashAlgorithm object to be used in flash calculations.</returns>
        /// <remarks></remarks>
        public  FlashAlgorithm FlashBase
        {
            get
            {
                return null;
                /*
        
                If Not My.Application.CAPEOPENMode And Not My.MyApplication.IsRunningParallelTasks Then
                    If Not this.Parameters.ContainsKey("PP_FLASHALGORITHM") Then
                        this.Parameters.Add("PP_FLASHALGORITHM", 2)
                    End If
                    this.FlashAlgorithm = this.Parameters("PP_FLASHALGORITHM")
                End If
                 switch FlashAlgorithm
                    case FlashMethod.DWSIMDefault
                        If _dwdf Is Nothing Then _dwdf = New Auxiliary.FlashAlgorithms.DWSIMDefault
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.DWSIMDefault Else return _dwdf
                    case FlashMethod.InsideOut
                        If _bbio Is Nothing Then _bbio = New Auxiliary.FlashAlgorithms.BostonBrittInsideOut
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.BostonBrittInsideOut Else return _bbio
                    case FlashMethod.InsideOut3P
                        If _brio3 Is Nothing Then _brio3 = New Auxiliary.FlashAlgorithms.BostonFournierInsideOut3P
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.BostonFournierInsideOut3P Else return _brio3
                    case FlashMethod.GibbsMin2P
                        If _gm3 Is Nothing Then _gm3 = New Auxiliary.FlashAlgorithms.GibbsMinimization3P
                        _gm3.ForceTwoPhaseOnly = True
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.GibbsMinimization3P With {.ForceTwoPhaseOnly = True} Else return _gm3
                    case FlashMethod.GibbsMin3P
                        If _gm3 Is Nothing Then _gm3 = New Auxiliary.FlashAlgorithms.GibbsMinimization3P
                        _gm3.ForceTwoPhaseOnly = False
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.GibbsMinimization3P With {.ForceTwoPhaseOnly = False} Else return _gm3
                    case FlashMethod.NestedLoops3P
                        If _nl3 Is Nothing Then _nl3 = New Auxiliary.FlashAlgorithms.NestedLoops3P
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.NestedLoops3P Else return _nl3
                    case FlashMethod.NestedLoops3PV2
                        If _nl3v2 Is Nothing Then _nl3v2 = New Auxiliary.FlashAlgorithms.NestedLoops3PV2
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.NestedLoops3P Else return _nl3v2
                    case FlashMethod.NestedLoopsSLE
                        If _nlsle Is Nothing Then _nlsle = New Auxiliary.FlashAlgorithms.NestedLoopsSLE
                        Dim constprops As New List(Of ConstantProperties)
                        For Each su As Substancia In this.CurrentMaterialStream.Fases(0).Componentes.Values
                            constprops.Add(su.ConstantProperties)
                        Next
                        _nlsle.CompoundProperties = constprops
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.NestedLoopsSLE With {.CompoundProperties = constprops} Else return _nlsle
                    case FlashMethod.NestedLoopsSLE_SS
                        If _nlsle Is Nothing Then _nlsle = New Auxiliary.FlashAlgorithms.NestedLoopsSLE With {.SolidSolution = True}
                        Dim constprops As New List(Of ConstantProperties)
                        For Each su As Substancia In this.CurrentMaterialStream.Fases(0).Componentes.Values
                            constprops.Add(su.ConstantProperties)
                        Next
                        _nlsle.CompoundProperties = constprops
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.NestedLoopsSLE With {.CompoundProperties = constprops, .SolidSolution = True} Else return _nlsle
                    case FlashMethod.NestedLoopsImmiscible
                        If _nli Is Nothing Then _nli = New Auxiliary.FlashAlgorithms.NestedLoopsImmiscible
                        Dim constprops As New List(Of ConstantProperties)
                        For Each su As Substancia In this.CurrentMaterialStream.Fases(0).Componentes.Values
                            constprops.Add(su.ConstantProperties)
                        Next
                        _nli.CompoundProperties = constprops
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.NestedLoopsImmiscible With {.CompoundProperties = constprops} Else return _nli
                    case Else
                        If _dwdf Is Nothing Then _dwdf = New Auxiliary.FlashAlgorithms.DWSIMDefault
                        If My.MyApplication.IsRunningParallelTasks Then return New Auxiliary.FlashAlgorithms.DWSIMDefault Else return _dwdf
                        return _dwdf
                End Select*/
            }
        }

        public string UniqueID
        {
            get
            {
                return _uniqueID;
            }
            set
            {
                _uniqueID = value;
            }
        }


        public string Tag
        {
            get
            {
                if(_tag == "")
                   return this.ComponentName; 
                else 
                    return _tag;
            }
            set
            {
                _tag = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public readonly PackageType PackageType
        {
            get
            {
                return _packagetype;
            }
        }

        public DataTable ParametrosDeInteracao
        {
            get
            {
                   return m_ip; 
            }
            set
            {
                m_ip = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public readonly Dictionary<string, double> Parameters
        {
            get
            {
                if (m_par == null)
                    ConfigParameters();
                return m_par;
            }
        }


        public bool IsConfigurable
        {
            get
            {
                   return m_configurable; 
            }
            set
            {
                m_configurable = value;
            }
        }



        /// <summary>
        /// Gets or sets the current material stream for this property package.
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public MaterialStream CurrentMaterialStream
        {
            get
            {
                   return m_ms; 
            }
            set
            {
                m_ms = value;
            }
        }

        public List<string> SupportedComponents
        {
            get
            {
                   return m_ss; 
            }
        }



        public object Clone() 
        {
            return null;    
            //modif adel
            //return ObjectCopy(Me)
        }

        /*
        Function ObjectCopy(ByVal obj As Object) As Object

            Dim objMemStream As New MemoryStream(50000)
            Dim objBinaryFormatter As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))

            objBinaryFormatter.Serialize(objMemStream, obj)

            objMemStream.Seek(0, SeekOrigin.Begin)

            ObjectCopy = objBinaryFormatter.Deserialize(objMemStream)

            objMemStream.Close()
        End Function
        */

        public virtual void  ShowConfigForm() {}

        public virtual void  ReconfigureConfigForm() {}


        /// <summary>
        /// Provides a wrapper function for CAPE-OPEN CalcProp/CalcSingleProp functions.
        /// </summary>
        /// <param name="property">The property to be calculated.</param>
        /// <param name="phase">The phase where the property must be calculated for.</param>
        /// <remarks>This function is necessary since DWSIM's internal property calculation function calculates all properties at once,
        /// while the CAPE-OPEN standards require that only the property that was asked for to be calculated, leaving the others unchanged.</remarks>
        public abstract void DW_CalcProp(string property, Fase phase );

        
        //modif adel
        /*
        /// <summary>
        /// Provides a default implementation for solid phase property calculations in CAPE-OPEN mode. Should be used by all derived propety packages.
        /// </summary>
        /// <remarks></remarks>
        public virtual void DW_CalcSolidPhaseProps()

            Dim phaseID As Integer = 7
            Dim result As Double = 0.0#

            Dim T, P As Double
            T = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature
            P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure

            result = this.AUX_SOLIDDENS
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.density = result
            Dim constprops As New List(Of ConstantProperties)
            For Each su As Substancia In this.CurrentMaterialStream.Fases(0).Componentes.Values
                constprops.Add(su.ConstantProperties)
            Next
            result = this.DW_CalcSolidEnthalpy(T, RET_VMOL(PropertyPackages.Fase.Solid), constprops)
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.enthalpy = result
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.entropy = result / T
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.compressibilityFactor = 0.0#
            result = this.DW_CalcSolidHeatCapacityCp(T, RET_VMOL(PropertyPackages.Fase.Solid), constprops)
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.heatCapacityCp = result
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.heatCapacityCv = result
            result = this.AUX_MMM(Fase.Solid)
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molecularWeight = result
            result = this.CurrentMaterialStream.Fases(phaseID).SPMProperties.enthalpy.GetValueOrDefault * this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molecularWeight.GetValueOrDefault
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_enthalpy = result
            result = this.CurrentMaterialStream.Fases(phaseID).SPMProperties.entropy.GetValueOrDefault * this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molecularWeight.GetValueOrDefault
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_entropy = result
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.thermalConductivity = 0.0#
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.viscosity = 1.0E+20
            this.CurrentMaterialStream.Fases(phaseID).SPMProperties.kinematic_viscosity = 1.0E+20
        End void
        */
        /// <summary>
        /// Calculates the enthalpy of a mixture.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing the molar composition of the mixture.</param>
        /// <param name="T">Temperature (K)</param>
        /// <param name="P">Pressure (Pa)</param>
        /// <param name="st">State enum indicating the state of the mixture (liquid or vapor).</param>
        /// <returns>The enthalpy of the mixture in kJ/kg.</returns>
        /// <remarks>The basis for the calculated enthalpy/entropy in DWSIM is zero at 25 C and 1 atm.</remarks>
        public abstract double DW_CalcEnthalpy(Array Vx , double T , double P, State st ); 

        /// <summary>
        /// Calculates the enthalpy departure of a mixture.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing the molar composition of the mixture.</param>
        /// <param name="T">Temperature (K)</param>
        /// <param name="P">Pressure (Pa)</param>
        /// <param name="st">State enum indicating the state of the mixture (liquid or vapor).</param>
        /// <returns>The enthalpy departure of the mixture in kJ/kg.</returns>
        /// <remarks>The basis for the calculated enthalpy/entropy in DWSIM is zero at 25 C and 1 atm.</remarks>
        public abstract double DW_CalcEnthalpyDeparture(Array Vx, double T, double P, State st);

        /// <summary>
        /// Calculates the entropy of a mixture.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing the molar composition of the mixture.</param>
        /// <param name="T">Temperature (K)</param>
        /// <param name="P">Pressure (Pa)</param>
        /// <param name="st">State enum indicating the state of the mixture (liquid or vapor).</param>
        /// <returns>The entropy of the mixture in kJ/kg.K.</returns>
        /// <remarks>The basis for the calculated enthalpy/entropy in DWSIM is zero at 25 C and 1 atm.</remarks>
        public abstract double DW_CalcEntropy(Array Vx, double T, double P, State st);

        /// <summary>
        /// Calculates the entropy departure of a mixture.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing the molar composition of the mixture.</param>
        /// <param name="T">Temperature (K)</param>
        /// <param name="P">Pressure (Pa)</param>
        /// <param name="st">State enum indicating the state of the mixture (liquid or vapor).</param>
        /// <returns>The entropy departure of the mixture in kJ/kg.K.</returns>
        /// <remarks>The basis for the calculated enthalpy/entropy in DWSIM is zero at 25 C and 1 atm.</remarks>
        public abstract double DW_CalcEntropyDeparture(Array Vx, double T, double P, State st);

        /// <summary>
        /// Calculates K-values of components in a mixture.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing the molar composition of the liquid phase.</param>
        /// <param name="Vy">Vector of doubles containing the molar composition of the vapor phase.</param>
        /// <param name="T">Temperature of the system.</param>
        /// <param name="P">Pressure of the system.</param>
        /// <returns>An array containing K-values for all components in the mixture.</returns>
        /// <remarks>The composition vector must follow the same sequence as the components which were added in the material stream.</remarks>
        public virtual object DW_CalcKvalue(Array Vx , Array Vy , double T, double P, string  type  = "LV") 
        {
            return null;
            //modif adel
            /*
            Dim fugvap As Object =null;
            Dim fugliq As Object =null;

            Dim alreadymt As Boolean = False

            If My.Settings.EnableParallelProcessing Then
                My.MyApplication.IsRunningParallelTasks = True
                Try
                    Dim task1 As Task = New Task(Sub()
                                                     fugliq = this.DW_CalcFugCoeff(Vx, T, P, State.Liquid)
                                                 End Sub)
                    Dim task2 As Task = New Task(Sub()
                                                     If type = "LV" Then
                                                         fugvap = this.DW_CalcFugCoeff(Vy, T, P, State.Vapor)
                                                     Else ' LL
                                                         fugvap = this.DW_CalcFugCoeff(Vy, T, P, State.Liquid)
                                                     End If
                                                 End Sub)

                    task1.Start()
                    task2.Start()
                    Task.WaitAll(task1, task2)
                Catch ae As AggregateException
                    For Each ex As Exception In ae.InnerExceptions
                        Throw ex
                    Next
                Finally
                End Try
                My.MyApplication.IsRunningParallelTasks = False
            Else
                fugliq = this.DW_CalcFugCoeff(Vx, T, P, State.Liquid)
                If type = "LV" Then
                    fugvap = this.DW_CalcFugCoeff(Vy, T, P, State.Vapor)
                Else ' LL
                    fugvap = this.DW_CalcFugCoeff(Vy, T, P, State.Liquid)
                End If
            End If

            Dim n As Integer = UBound(fugvap)
            Dim i As Integer
            Dim K(n) As Double

            For i = 0 To n
                K(i) = fugliq(i) / fugvap(i)
            Next

            i = 0
            For Each subst As ClassesBasicasTermodinamica.Substancia In this.CurrentMaterialStream.Fases(1).Componentes.Values
                If K(i) = 0 Or Double.IsInfinity(K(i)) Or Double.IsNaN(K(i)) Then
                    Dim Pc, Tc, w As Double
                    Pc = subst.ConstantProperties.Critical_Pressure
                    Tc = subst.ConstantProperties.Critical_Temperature
                    w = subst.ConstantProperties.Acentric_Factor
                    If type = "LV" Then
                        K(i) = Pc / P * Math.Exp(5.373 * (1 + w) * (1 - Tc / T))
                    Else
                        K(i) = 1.0#
                    End If
                End If
                i += 1
            Next

            If this.AUX_CheckTrivial(K) Then
                i = 0
                For Each subst As ClassesBasicasTermodinamica.Substancia In this.CurrentMaterialStream.Fases(1).Componentes.Values
                    Dim Pc, Tc, w As Double
                    Pc = subst.ConstantProperties.Critical_Pressure
                    Tc = subst.ConstantProperties.Critical_Temperature
                    w = subst.ConstantProperties.Acentric_Factor
                    If type = "LV" Then
                        K(i) = Pc / P * Math.Exp(5.373 * (1 + w) * (1 - Tc / T))
                    Else
                        K(i) = 1.0#
                    End If
                    i += 1
                Next
            End If

            return K
            */
        }

        /// <summary>
        /// Calculates K-values of components in a mixture.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing the molar composition of the mixture.</param>
        /// <param name="T">Temperature of the system, in K.</param>
        /// <param name="P">Pressure of the system, in Pa.</param>
        /// <returns>An array containing K-values for all components in the mixture.</returns>
        /// <remarks>The composition vector must follow the same sequence as the components which were added in the material stream.</remarks>
        public virtual object DW_CalcKvalue(Array Vx, double T, double P) 
        {
            //modif adel
            return null;
            /*
            Dim i As Integer
            Dim result = this.FlashBase.Flash_PT(Vx, P, T, Me)
            Dim n As Integer = UBound(Vx)
            Dim K(n) As Double

            i = 0
            For Each subst As ClassesBasicasTermodinamica.Substancia In this.CurrentMaterialStream.Fases(1).Componentes.Values
                K(i) = (result(3)(i) / result(2)(i))
                i += 1
            Next

            i = 0
            For Each subst As ClassesBasicasTermodinamica.Substancia In this.CurrentMaterialStream.Fases(1).Componentes.Values
                If K(i) = 0 Then K(i) = this.AUX_PVAPi(subst.Nome, T) / P
                If Double.IsInfinity(K(i)) Or Double.IsNaN(K(i)) Then
                    Dim Pc, Tc, w As Double
                    Pc = subst.ConstantProperties.Critical_Pressure
                    Tc = subst.ConstantProperties.Critical_Temperature
                    w = subst.ConstantProperties.Acentric_Factor
                    K(i) = Pc / P * Math.Exp(5.373 * (1 + w) * (1 - Tc / T))
                End If
                i += 1
            Next

            If this.AUX_CheckTrivial(K) Then
                For i = 0 To UBound(Vx)
                    K(i) = this.AUX_PVAPi(i, T) / P
                    i += 1
                Next
            End If

            return K
            */
        }

        /// <summary>
        /// Does a Bubble Pressure calculation for the specified liquid composition at the specified temperature.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing liquid phase molar composition for each component in the mixture.</param>
        /// <param name="T">Temperature in K</param>
        /// <param name="Pref">Initial estimate for Pressure, in Pa</param>
        /// <param name="K">Vector with initial estimates for K-values</param>
        /// <param name="ReuseK">Boolean indicating wether to use the initial estimates for K-values or not.</param>
        /// <returns>Returns the object vector {L, V, Vx, Vy, P, ecount, Ki} where L is the liquid phase molar fraction, 
        /// V is the vapor phase molar fraction, Vx is the liquid phase molar composition vector, Vy is the vapor phase molar 
        /// composition vector, P is the calculated Pressure in Pa, ecount is the number of iterations and Ki is a vector containing 
        /// the calculated K-values.</returns>
        /// <remarks>The composition vector must follow the same sequence as the components which were added in the material stream.</remarks>
        public virtual object DW_CalcBubP(Array Vx, double T, double Pref = 0, Array K  = null, bool ReuseK  = false) 
        {
            return  FlashBase.Flash_TV(Vx, T, 0, Pref, this, ReuseK, K);
        }
        

        /// <summary>
        /// Does a Bubble Temperature calculation for the specified liquid composition at the specified pressure.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing liquid phase molar composition for each component in the mixture.</param>
        /// <param name="P"></param>
        /// <param name="Tref"></param>
        /// <param name="K">Vector with initial estimates for K-values</param>
        /// <param name="ReuseK">Boolean indicating wether to use the initial estimates for K-values or not.</param>
        /// <returns>Returns the object vector {L, V, Vx, Vy, T, ecount, Ki} where L is the liquid phase molar fraction, 
        /// V is the vapor phase molar fraction, Vx is the liquid phase molar composition vector, Vy is the vapor phase molar 
        /// composition vector, T is the calculated Temperature in K, ecount is the number of iterations and Ki is a vector containing 
        /// the calculated K-values.</returns>
        /// <remarks>The composition vector must follow the same sequence as the components which were added in the material stream.</remarks>
        public virtual object DW_CalcBubT(Array Vx, double P, double Tref = 0, Array K  = null, bool ReuseK  = false)
        { 
            return FlashBase.Flash_PV(Vx, P, 0, Tref, this, ReuseK, K);
        }

        /// <summary>
        /// Does a Dew Pressure calculation for the specified vapor phase composition at the specified temperature.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing vapor phase molar composition for each component in the mixture.</param>
        /// <param name="T">Temperature in K</param>
        /// <param name="Pref">Initial estimate for Pressure, in Pa</param>
        /// <param name="K">Vector with initial estimates for K-values</param>
        /// <param name="ReuseK">Boolean indicating wether to use the initial estimates for K-values or not.</param>
        /// <returns>Returns the object vector {L, V, Vx, Vy, P, ecount, Ki} where L is the liquid phase molar fraction, 
        /// V is the vapor phase molar fraction, Vx is the liquid phase molar composition vector, Vy is the vapor phase molar 
        /// composition vector, P is the calculated Pressure in Pa, ecount is the number of iterations and Ki is a vector containing 
        /// the calculated K-values.</returns>
        /// <remarks>The composition vector must follow the same sequence as the components which were added in the material stream.</remarks>
        public virtual object DW_CalcDewP(Array Vx, double T, double Pref = 0, Array K  = null, bool ReuseK  = false) 
        {
            return FlashBase.Flash_TV(Vx, T, 1, Pref, this, ReuseK, K);
        }

        /// <summary>
        /// Does a Dew Temperature calculation for the specified vapor composition at the specified pressure.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing vapor phase molar composition for each component in the mixture.</param>
        /// <param name="P"></param>
        /// <param name="Tref"></param>
        /// <param name="K">Vector with initial estimates for K-values</param>
        /// <param name="ReuseK">Boolean indicating wether to use the initial estimates for K-values or not.</param>
        /// <returns>Returns the object vector {L, V, Vx, Vy, T, ecount, Ki} where L is the liquid phase molar fraction, 
        /// V is the vapor phase molar fraction, Vx is the liquid phase molar composition vector, Vy is the vapor phase molar 
        /// composition vector, T is the calculated Temperature in K, ecount is the number of iterations and Ki is a vector containing 
        /// the calculated K-values.</returns>
        /// <remarks>The composition vector must follow the same sequence as the components which were added in the material stream.</remarks>
        public virtual object DW_CalcDewT(Array Vx, double P, double Tref = 0, Array K  = null, bool ReuseK  = false)
        {
            return  FlashBase.Flash_PV(Vx, P, 1, Tref, this, ReuseK, K);
        }

        /// <summary>
        /// Calculates fugacity coefficients for the specified composition at the specified conditions.
        /// </summary>
        /// <param name="Vx">Vector of doubles containing the molar composition of the mixture.</param>
        /// <param name="T">Temperature in K</param>
        /// <param name="P">Pressure in Pa</param>
        /// <param name="st">Mixture state (Liquid or Vapor)</param>
        /// <returns>A vector of doubles containing fugacity coefficients for the components in the mixture.</returns>
        /// <remarks>The composition vector must follow the same sequence as the components which were added in the material stream.</remarks>
        public abstract object DW_CalcFugCoeff(Array Vx, double T, double P, State st) ;

        public abstract bool SupportsComponent(ConstantProperties comp ); 

        public abstract void DW_CalcPhaseProps(Fase fase );

        public virtual void DW_CalcTwoPhaseProps(Fase fase1  , Fase fase2  )
        {
            //modif adel
            /*
            Dim T As Double

            T = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature
            this.CurrentMaterialStream.Fases(0).TPMProperties.surfaceTension = this.AUX_SURFTM(T)*/
        }

        public double DW_CalcGibbsEnergy(Array Vx, double T, double P) 
        {
            return 0.0;
            //modif adel
            /*
            Dim fugvap As Object =null;
            Dim fugliq As Object =null;

            If My.Settings.EnableParallelProcessing Then
                My.MyApplication.IsRunningParallelTasks = True
                Try
                    Dim task1 As Task = New Task(Sub()
                                                     fugliq = this.DW_CalcFugCoeff(Vx, T, P, State.Liquid)
                                                 End Sub)
                    Dim task2 As Task = New Task(Sub()
                                                     fugvap = this.DW_CalcFugCoeff(Vx, T, P, State.Vapor)
                                                 End Sub)
                    task1.Start()
                    task2.Start()
                    Task.WaitAll(task1, task2)
                Catch ae As AggregateException
                    For Each ex As Exception In ae.InnerExceptions
                        Throw ex
                    Next
                Finally
                End Try
                My.MyApplication.IsRunningParallelTasks = False
            Else
                fugliq = this.DW_CalcFugCoeff(Vx, T, P, State.Liquid)
                fugvap = this.DW_CalcFugCoeff(Vx, T, P, State.Vapor)
            End If

            Dim n As Integer = UBound(Vx)
            Dim i As Integer

            Dim g, gid, gexv, gexl As Double

            gid = 0.0#

            'If MathEx.Common.Sum(Vx) <> 0.0# Then
            '    gid = RET_Gid(298.15, T, P, Vx) * AUX_MMM(Vx) 'kJ/kmol
            'End If

            gexv = 0.0#
            gexl = 0.0#
            For i = 0 To n
                If Vx(i) <> 0.0# Then gexv += Vx(i) * Log(fugvap(i)) * 8.314 * T
                If Vx(i) <> 0.0# Then gexl += Vx(i) * Log(fugliq(i)) * 8.314 * T
            Next

            If gexv < gexl Then g = gid + gexv Else g = gid + gexl

            return g 'kJ/kmol*/
        }

        public virtual void DW_CalcOverallProps()
        {
            /*
            Dim HL, HV, HS, SL, SV, SS, DL, DV, DS, CPL, CPV, CPS, KL, KV, KS, CVL, CVV, CSV As Nullable(Of Double)
            Dim xl, xv, xs, wl, wv, ws, vl, vv, vs, result As Double

            xl = this.CurrentMaterialStream.Fases(1).SPMProperties.molarfraction.GetValueOrDefault
            xv = this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction.GetValueOrDefault
            xs = this.CurrentMaterialStream.Fases(7).SPMProperties.molarfraction.GetValueOrDefault

            wl = this.CurrentMaterialStream.Fases(1).SPMProperties.massfraction.GetValueOrDefault
            wv = this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault
            ws = this.CurrentMaterialStream.Fases(7).SPMProperties.massfraction.GetValueOrDefault

            DL = this.CurrentMaterialStream.Fases(1).SPMProperties.density.GetValueOrDefault
            DV = this.CurrentMaterialStream.Fases(2).SPMProperties.density.GetValueOrDefault
            DS = this.CurrentMaterialStream.Fases(7).SPMProperties.density.GetValueOrDefault

            Dim tl As Double = 0.0#
            Dim tv As Double = 0.0#
            Dim ts As Double = 0.0#

            If DL <> 0.0# Then tl = this.CurrentMaterialStream.Fases(1).SPMProperties.massfraction.GetValueOrDefault / DL.GetValueOrDefault
            If DV <> 0.0# Then tv = this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault / DV.GetValueOrDefault
            If DS <> 0.0# Then ts = this.CurrentMaterialStream.Fases(7).SPMProperties.massfraction.GetValueOrDefault / DS.GetValueOrDefault

            vl = tl / (tl + tv + ts)
            vv = tv / (tl + tv + ts)
            vs = ts / (tl + tv + ts)

            If xl = 1 Then
                vl = 1
                vv = 0
            ElseIf xl = 0 Then
                vl = 0
                vv = 1
            End If

            result = vl * DL.GetValueOrDefault + vv * DV.GetValueOrDefault + vs * DS.GetValueOrDefault
            If Double.IsNaN(result) Then
                If Double.IsNaN(DL) = False And Double.IsNaN(DV) = True Then
                    result = DL
                ElseIf Double.IsNaN(DL) = True And Double.IsNaN(DV) = False Then
                    result = DV
                Else
                    result = 0
                End If
            End If
            this.CurrentMaterialStream.Fases(0).SPMProperties.density = result

            HL = this.CurrentMaterialStream.Fases(1).SPMProperties.enthalpy.GetValueOrDefault
            HV = this.CurrentMaterialStream.Fases(2).SPMProperties.enthalpy.GetValueOrDefault
            HS = this.CurrentMaterialStream.Fases(7).SPMProperties.enthalpy.GetValueOrDefault

            result = wl * HL.GetValueOrDefault + wv * HV.GetValueOrDefault + ws * HS.GetValueOrDefault
            this.CurrentMaterialStream.Fases(0).SPMProperties.enthalpy = result

            SL = this.CurrentMaterialStream.Fases(1).SPMProperties.entropy.GetValueOrDefault
            SV = this.CurrentMaterialStream.Fases(2).SPMProperties.entropy.GetValueOrDefault
            SS = this.CurrentMaterialStream.Fases(7).SPMProperties.entropy.GetValueOrDefault

            result = wl * SL.GetValueOrDefault + wv * SV.GetValueOrDefault + ws * SS.GetValueOrDefault
            this.CurrentMaterialStream.Fases(0).SPMProperties.entropy = result

            this.CurrentMaterialStream.Fases(0).SPMProperties.compressibilityFactor =null;

            CPL = this.CurrentMaterialStream.Fases(1).SPMProperties.heatCapacityCp.GetValueOrDefault
            CPV = this.CurrentMaterialStream.Fases(2).SPMProperties.heatCapacityCp.GetValueOrDefault
            CPS = this.CurrentMaterialStream.Fases(7).SPMProperties.heatCapacityCp.GetValueOrDefault

            result = wl * CPL.GetValueOrDefault + wv * CPV.GetValueOrDefault + ws * CPS.GetValueOrDefault
            this.CurrentMaterialStream.Fases(0).SPMProperties.heatCapacityCp = result

            CVL = this.CurrentMaterialStream.Fases(1).SPMProperties.heatCapacityCv.GetValueOrDefault
            CVV = this.CurrentMaterialStream.Fases(2).SPMProperties.heatCapacityCv.GetValueOrDefault
            CSV = this.CurrentMaterialStream.Fases(7).SPMProperties.heatCapacityCv.GetValueOrDefault

            result = wl * CVL.GetValueOrDefault + wv * CVV.GetValueOrDefault + ws * CSV.GetValueOrDefault
            this.CurrentMaterialStream.Fases(0).SPMProperties.heatCapacityCv = result

            result = this.AUX_MMM(Fase.Mixture)
            this.CurrentMaterialStream.Fases(0).SPMProperties.molecularWeight = result

            result = this.CurrentMaterialStream.Fases(0).SPMProperties.enthalpy.GetValueOrDefault * this.CurrentMaterialStream.Fases(0).SPMProperties.molecularWeight.GetValueOrDefault
            this.CurrentMaterialStream.Fases(0).SPMProperties.molar_enthalpy = result
            result = this.CurrentMaterialStream.Fases(0).SPMProperties.entropy.GetValueOrDefault * this.CurrentMaterialStream.Fases(0).SPMProperties.molecularWeight.GetValueOrDefault
            this.CurrentMaterialStream.Fases(0).SPMProperties.molar_entropy = result

            KL = this.CurrentMaterialStream.Fases(1).SPMProperties.thermalConductivity.GetValueOrDefault
            KV = this.CurrentMaterialStream.Fases(2).SPMProperties.thermalConductivity.GetValueOrDefault
            KS = this.CurrentMaterialStream.Fases(7).SPMProperties.thermalConductivity.GetValueOrDefault

            result = xl * KL.GetValueOrDefault + xv * KV.GetValueOrDefault + xs * KS.GetValueOrDefault
            this.CurrentMaterialStream.Fases(0).SPMProperties.thermalConductivity = result

            this.CurrentMaterialStream.Fases(0).SPMProperties.viscosity =null;

            this.CurrentMaterialStream.Fases(0).SPMProperties.kinematic_viscosity =null;
            */
        }
 
        public virtual void DW_CalcLiqMixtureProps()
        {
            /*
            Dim hl, hl1, hl2, hl3, hw, sl, sl1, sl2, sl3, sw, dl, dl1, dl2, dl3, dw As Double
            Dim cpl, cpl1, cpl2, cpl3, cpw, cvl, cvl1, cvl2, cvl3, cvw As Double
            Dim kl, kl1, kl2, kl3, kw, vil, vil1, vil2, vil3, viw As Double
            Dim xl, xl1, xl2, xl3, xw, wl, wl1, wl2, wl3, ww As Double
            Dim xlf, xlf1, xlf2, xlf3, xwf, wlf, wlf1, wlf2, wlf3, wwf As Double
            Dim cml, cml1, cml2, cml3, cmw, cwl, cwl1, cwl2, cwl3, cww As Double

            xl1 = this.CurrentMaterialStream.Fases(3).SPMProperties.molarfraction.GetValueOrDefault
            xl2 = this.CurrentMaterialStream.Fases(4).SPMProperties.molarfraction.GetValueOrDefault
            xl3 = this.CurrentMaterialStream.Fases(5).SPMProperties.molarfraction.GetValueOrDefault
            xw = this.CurrentMaterialStream.Fases(6).SPMProperties.molarfraction.GetValueOrDefault

            xl = xl1 + xl2 + xl3 + xw
            this.CurrentMaterialStream.Fases(1).SPMProperties.molarfraction = xl

            wl1 = this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction.GetValueOrDefault
            wl2 = this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction.GetValueOrDefault
            wl3 = this.CurrentMaterialStream.Fases(5).SPMProperties.massfraction.GetValueOrDefault
            ww = this.CurrentMaterialStream.Fases(6).SPMProperties.massfraction.GetValueOrDefault

            wl = wl1 + wl2 + wl3 + ww
            this.CurrentMaterialStream.Fases(1).SPMProperties.massfraction = wl

            xlf1 = this.CurrentMaterialStream.Fases(3).SPMProperties.molarflow.GetValueOrDefault
            xlf2 = this.CurrentMaterialStream.Fases(4).SPMProperties.molarflow.GetValueOrDefault
            xlf3 = this.CurrentMaterialStream.Fases(5).SPMProperties.molarflow.GetValueOrDefault
            xwf = this.CurrentMaterialStream.Fases(6).SPMProperties.molarflow.GetValueOrDefault

            xlf = xlf1 + xlf2 + xlf3 + xwf
            this.CurrentMaterialStream.Fases(1).SPMProperties.molarflow = xlf

            wlf1 = this.CurrentMaterialStream.Fases(3).SPMProperties.massflow.GetValueOrDefault
            wlf2 = this.CurrentMaterialStream.Fases(4).SPMProperties.massflow.GetValueOrDefault
            wlf3 = this.CurrentMaterialStream.Fases(5).SPMProperties.massflow.GetValueOrDefault
            wwf = this.CurrentMaterialStream.Fases(6).SPMProperties.massflow.GetValueOrDefault

            wlf = wlf1 + wlf2 + wlf3 + wwf
            this.CurrentMaterialStream.Fases(1).SPMProperties.massflow = wlf

            For Each c As Substancia In this.CurrentMaterialStream.Fases(1).Componentes.Values
                cml1 = this.CurrentMaterialStream.Fases(3).Componentes(c.Nome).FracaoMolar.GetValueOrDefault
                cml2 = this.CurrentMaterialStream.Fases(4).Componentes(c.Nome).FracaoMolar.GetValueOrDefault
                cml3 = this.CurrentMaterialStream.Fases(5).Componentes(c.Nome).FracaoMolar.GetValueOrDefault
                cmw = this.CurrentMaterialStream.Fases(6).Componentes(c.Nome).FracaoMolar.GetValueOrDefault
                cwl1 = this.CurrentMaterialStream.Fases(3).Componentes(c.Nome).FracaoMassica.GetValueOrDefault
                cwl2 = this.CurrentMaterialStream.Fases(4).Componentes(c.Nome).FracaoMassica.GetValueOrDefault
                cwl3 = this.CurrentMaterialStream.Fases(5).Componentes(c.Nome).FracaoMassica.GetValueOrDefault
                cww = this.CurrentMaterialStream.Fases(6).Componentes(c.Nome).FracaoMassica.GetValueOrDefault
                cml = (xl1 * cml1 + xl2 * cml2 + xl3 * cml3 + xw * cmw) / xl
                cwl = (wl1 * cwl1 + wl2 * cwl2 + wl3 * cwl3 + ww * cww) / wl
                c.FracaoMolar = cml
                c.FracaoMassica = cwl
            Next

            If wl1 > 0 Then dl1 = this.CurrentMaterialStream.Fases(3).SPMProperties.density.GetValueOrDefault Else dl1 = 1
            If wl2 > 0 Then dl2 = this.CurrentMaterialStream.Fases(4).SPMProperties.density.GetValueOrDefault Else dl2 = 1
            If wl3 > 0 Then dl3 = this.CurrentMaterialStream.Fases(5).SPMProperties.density.GetValueOrDefault Else dl3 = 1
            If ww > 0 Then dw = this.CurrentMaterialStream.Fases(6).SPMProperties.density.GetValueOrDefault Else dw = 1

            dl = wl1 / dl1 + wl2 / dl2 + wl3 / dl3 + ww / dw
            dl = wl / dl
            this.CurrentMaterialStream.Fases(1).SPMProperties.density = dl


            If Double.IsNaN(wlf / dl) Then
                this.CurrentMaterialStream.Fases(1).SPMProperties.volumetric_flow = 0.0#
            Else
                this.CurrentMaterialStream.Fases(1).SPMProperties.volumetric_flow = wlf / dl
            End If

            If wl = 0 Then wl = 1.0#

            hl1 = this.CurrentMaterialStream.Fases(3).SPMProperties.enthalpy.GetValueOrDefault
            hl2 = this.CurrentMaterialStream.Fases(4).SPMProperties.enthalpy.GetValueOrDefault
            hl3 = this.CurrentMaterialStream.Fases(5).SPMProperties.enthalpy.GetValueOrDefault
            hw = this.CurrentMaterialStream.Fases(6).SPMProperties.enthalpy.GetValueOrDefault

            hl = hl1 * wl1 + hl2 * wl2 + hl3 * wl3 + hw * ww

            this.CurrentMaterialStream.Fases(1).SPMProperties.enthalpy = hl / wl

            sl1 = this.CurrentMaterialStream.Fases(3).SPMProperties.entropy.GetValueOrDefault
            sl2 = this.CurrentMaterialStream.Fases(4).SPMProperties.entropy.GetValueOrDefault
            sl3 = this.CurrentMaterialStream.Fases(5).SPMProperties.entropy.GetValueOrDefault
            sw = this.CurrentMaterialStream.Fases(6).SPMProperties.entropy.GetValueOrDefault

            sl = sl1 * wl1 + sl2 * wl2 + sl3 * wl3 + sw * ww

            this.CurrentMaterialStream.Fases(1).SPMProperties.entropy = sl / wl

            cpl1 = this.CurrentMaterialStream.Fases(3).SPMProperties.heatCapacityCp.GetValueOrDefault
            cpl2 = this.CurrentMaterialStream.Fases(4).SPMProperties.heatCapacityCp.GetValueOrDefault
            cpl3 = this.CurrentMaterialStream.Fases(5).SPMProperties.heatCapacityCp.GetValueOrDefault
            cpw = this.CurrentMaterialStream.Fases(6).SPMProperties.heatCapacityCp.GetValueOrDefault

            cpl = cpl1 * wl1 + cpl2 * wl2 + cpl3 * wl3 + cpw * ww

            this.CurrentMaterialStream.Fases(1).SPMProperties.heatCapacityCp = cpl / wl

            cvl1 = this.CurrentMaterialStream.Fases(3).SPMProperties.heatCapacityCv.GetValueOrDefault
            cvl2 = this.CurrentMaterialStream.Fases(4).SPMProperties.heatCapacityCv.GetValueOrDefault
            cvl3 = this.CurrentMaterialStream.Fases(5).SPMProperties.heatCapacityCv.GetValueOrDefault
            cvw = this.CurrentMaterialStream.Fases(6).SPMProperties.heatCapacityCv.GetValueOrDefault

            cvl = cvl1 * wl1 + cvl2 * wl2 + cvl3 * wl3 + cvw * ww

            this.CurrentMaterialStream.Fases(1).SPMProperties.heatCapacityCv = cvl / wl

            Dim result As Double

            result = this.AUX_MMM(Fase.Liquid)
            this.CurrentMaterialStream.Fases(1).SPMProperties.molecularWeight = result

            result = this.CurrentMaterialStream.Fases(1).SPMProperties.enthalpy.GetValueOrDefault * this.CurrentMaterialStream.Fases(1).SPMProperties.molecularWeight.GetValueOrDefault
            this.CurrentMaterialStream.Fases(1).SPMProperties.molar_enthalpy = result

            result = this.CurrentMaterialStream.Fases(1).SPMProperties.entropy.GetValueOrDefault * this.CurrentMaterialStream.Fases(1).SPMProperties.molecularWeight.GetValueOrDefault
            this.CurrentMaterialStream.Fases(1).SPMProperties.molar_entropy = result

            kl1 = this.CurrentMaterialStream.Fases(3).SPMProperties.thermalConductivity.GetValueOrDefault
            kl2 = this.CurrentMaterialStream.Fases(4).SPMProperties.thermalConductivity.GetValueOrDefault
            kl3 = this.CurrentMaterialStream.Fases(5).SPMProperties.thermalConductivity.GetValueOrDefault
            kw = this.CurrentMaterialStream.Fases(6).SPMProperties.thermalConductivity.GetValueOrDefault

            kl = kl1 * wl1 + kl2 * wl2 + kl3 * kl3 + kw * ww

            this.CurrentMaterialStream.Fases(1).SPMProperties.thermalConductivity = kl / wl

            vil1 = this.CurrentMaterialStream.Fases(3).SPMProperties.viscosity.GetValueOrDefault
            vil2 = this.CurrentMaterialStream.Fases(4).SPMProperties.viscosity.GetValueOrDefault
            vil3 = this.CurrentMaterialStream.Fases(5).SPMProperties.viscosity.GetValueOrDefault
            viw = this.CurrentMaterialStream.Fases(6).SPMProperties.viscosity.GetValueOrDefault

            vil = vil1 * wl1 + vil2 * vil2 + kl3 * vil3 + viw * ww

            this.CurrentMaterialStream.Fases(1).SPMProperties.viscosity = vil / wl

            this.CurrentMaterialStream.Fases(1).SPMProperties.kinematic_viscosity = vil / dl

            this.CurrentMaterialStream.Fases(1).SPMProperties.compressibilityFactor = 0.0#
            */
        }

        public virtual void DW_CalcEquilibrium(FlashSpec spec1, FlashSpec spec2)    
        {
            /*
            this.CurrentMaterialStream.AtEquilibrium = False

            Try
                If this._brio3 Is Nothing Then this._brio3 = New Auxiliary.FlashAlgorithms.BostonFournierInsideOut3P
                If this._nl3 Is Nothing Then this._nl3 = New Auxiliary.FlashAlgorithms.NestedLoops3P
                If this._nl3v2 Is Nothing Then this._nl3v2 = New Auxiliary.FlashAlgorithms.NestedLoops3PV2
                If this._nli Is Nothing Then this._nli = New Auxiliary.FlashAlgorithms.NestedLoopsImmiscible
                this._brio3.StabSearchCompIDs = _tpcompids
                this._brio3.StabSearchSeverity = _tpseverity
                this._nl3.StabSearchCompIDs = _tpcompids
                this._nl3.StabSearchSeverity = _tpseverity
                this._nl3v2.StabSearchCompIDs = _tpcompids
                this._nl3v2.StabSearchSeverity = _tpseverity
                this._nli.StabSearchCompIDs = _tpcompids
                this._nli.StabSearchSeverity = _tpseverity
            Catch ex As Exception
            End Try

            Dim P, T, H, S, xv, xl, xl2, xs As Double
            Dim result As Object =null;
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim n As Integer = this.CurrentMaterialStream.Fases(0).Componentes.Count
            Dim i As Integer = 0

            'for TVF/PVF/PH/PS flashes
            xv = this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction.GetValueOrDefault
            H = this.CurrentMaterialStream.Fases(0).SPMProperties.enthalpy.GetValueOrDefault
            S = this.CurrentMaterialStream.Fases(0).SPMProperties.entropy.GetValueOrDefault

            this.DW_ZerarPhaseProps(Fase.Vapor)
            this.DW_ZerarPhaseProps(Fase.Liquid)
            this.DW_ZerarPhaseProps(Fase.Liquid1)
            this.DW_ZerarPhaseProps(Fase.Liquid2)
            this.DW_ZerarPhaseProps(Fase.Liquid3)
            this.DW_ZerarPhaseProps(Fase.Aqueous)
            this.DW_ZerarPhaseProps(Fase.Solid)
            this.DW_ZerarComposicoes(Fase.Vapor)
            this.DW_ZerarComposicoes(Fase.Liquid)
            this.DW_ZerarComposicoes(Fase.Liquid1)
            this.DW_ZerarComposicoes(Fase.Liquid2)
            this.DW_ZerarComposicoes(Fase.Liquid3)
            this.DW_ZerarComposicoes(Fase.Aqueous)
            this.DW_ZerarComposicoes(Fase.Solid)

             switch spec1

                case FlashSpec.T

                     switch spec2

                        case FlashSpec.P

                            T = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature.GetValueOrDefault
                            P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure.GetValueOrDefault

                            Dim ige As Double = 0
                            Dim fge As Double = 0
                            Dim dge As Double = 0

                            If Not My.Application.CAPEOPENMode Then
                                If Not this.FlashAlgorithm = FlashMethod.NestedLoopsSLE _
                                And Not this.FlashAlgorithm = FlashMethod.NestedLoopsSLE_SS Then

                                    ige = this.DW_CalcGibbsEnergy(RET_VMOL(Fase.Mixture), T, P)

                                End If
                            End If

                            result = this.FlashBase.Flash_PT(RET_VMOL(Fase.Mixture), P, T, Me)

                            xl = result(0)
                            xv = result(1)
                            xl2 = result(5)
                            xs = result(7)

                            Dim Vx = result(2)
                            Dim Vy = result(3)
                            Dim Vx2 = result(6)
                            Dim Vs = result(8)

                            If Not My.Application.CAPEOPENMode Then
                                If Not this.FlashAlgorithm = FlashMethod.NestedLoopsSLE _
                                And Not this.FlashAlgorithm = FlashMethod.NestedLoopsSLE_SS Then

                                    fge = xl * this.DW_CalcGibbsEnergy(Vx, T, P)
                                    fge += xl2 * this.DW_CalcGibbsEnergy(Vx2, T, P)
                                    fge += xv * this.DW_CalcGibbsEnergy(Vy, T, P)

                                    dge = fge - ige

                                    Dim dgtol As Double = 0.01

                                    If dge > 0.0# And Math.Abs(dge / ige * 100) > Math.Abs(dgtol) Then
                                        Throw New Exception(DTL.App.GetLocalString("InvalidFlashResult") & "(DGE = " & dge & " kJ/kg, " & Format(dge / ige * 100, "0.00") & "%)")
                                    End If

                                End If
                            End If

                            'do a density calculation check to order liquid phases from lighter to heavier

                            If xl2 <> 0.0# And xl = 0.0# Then
                                xl = result(5)
                                xl2 = 0.0#
                                Vx = result(6)
                                Vx2 = result(2)
                            ElseIf xl2 <> 0.0# And xl <> 0.0# Then
                                Dim dens1, dens2 As Double
                                dens1 = this.AUX_LIQDENS(T, Vx, P, 0, False)
                                dens2 = this.AUX_LIQDENS(T, Vx2, P, 0, False)
                                If dens2 < dens1 Then
                                    xl = result(5)
                                    xl2 = result(0)
                                    Vx = result(6)
                                    Vx2 = result(2)
                                End If
                            End If

                            this.CurrentMaterialStream.Fases(3).SPMProperties.molarfraction = xl
                            this.CurrentMaterialStream.Fases(4).SPMProperties.molarfraction = xl2
                            this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction = xv
                            this.CurrentMaterialStream.Fases(7).SPMProperties.molarfraction = xs

                            Dim FCL = this.DW_CalcFugCoeff(Vx, T, P, State.Liquid)
                            Dim FCL2 = this.DW_CalcFugCoeff(Vx2, T, P, State.Liquid)
                            Dim FCV = this.DW_CalcFugCoeff(Vy, T, P, State.Vapor)
                            Dim FCS = this.DW_CalcFugCoeff(Vs, T, P, State.Solid)

                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                subst.FracaoMolar = Vx(i)
                                subst.FugacityCoeff = FCL(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 3)
                            Next
                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                subst.FracaoMolar = Vx2(i)
                                subst.FugacityCoeff = FCL2(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 4)
                            Next
                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                subst.FracaoMolar = Vy(i)
                                subst.FugacityCoeff = FCV(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 2)
                            Next
                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(7).Componentes.Values
                                subst.FracaoMolar = Vs(i)
                                subst.FugacityCoeff = FCS(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(7).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 7)
                            Next

                            this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction = xl * this.AUX_MMM(Fase.Liquid1) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor) + xs * this.AUX_MMM(Fase.Solid))
                            this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction = xl2 * this.AUX_MMM(Fase.Liquid2) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor) + xs * this.AUX_MMM(Fase.Solid))
                            this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction = xv * this.AUX_MMM(Fase.Vapor) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor) + xs * this.AUX_MMM(Fase.Solid))
                            this.CurrentMaterialStream.Fases(7).SPMProperties.massfraction = xs * this.AUX_MMM(Fase.Solid) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor) + xs * this.AUX_MMM(Fase.Solid))

                            Dim constprops As New List(Of ConstantProperties)
                            For Each su As Substancia In this.CurrentMaterialStream.Fases(0).Componentes.Values
                                constprops.Add(su.ConstantProperties)
                            Next

                            Dim HM, HV, HL, HL2, HS As Double

                            If xl <> 0 Then HL = this.DW_CalcEnthalpy(Vx, T, P, State.Liquid)
                            If xl2 <> 0 Then HL2 = this.DW_CalcEnthalpy(Vx2, T, P, State.Liquid)
                            If xv <> 0 Then HV = this.DW_CalcEnthalpy(Vy, T, P, State.Vapor)
                            If xs <> 0 Then HS = this.DW_CalcSolidEnthalpy(T, Vs, constprops)
                            HM = this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction.GetValueOrDefault * HL2 + this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction.GetValueOrDefault * HL + this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault * HV + this.CurrentMaterialStream.Fases(7).SPMProperties.massfraction.GetValueOrDefault * HS

                            H = HM

                            Dim SM, SV, SL, SL2, SS As Double

                            If xl <> 0 Then SL = this.DW_CalcEntropy(Vx, T, P, State.Liquid)
                            If xl2 <> 0 Then SL2 = this.DW_CalcEntropy(Vx2, T, P, State.Liquid)
                            If xv <> 0 Then SV = this.DW_CalcEntropy(Vy, T, P, State.Vapor)
                            If xs <> 0 Then SS = this.DW_CalcSolidEnthalpy(T, Vs, constprops) / (T - 298.15)
                            SM = this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction.GetValueOrDefault * SL2 + this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction.GetValueOrDefault * SL + this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault * SV + this.CurrentMaterialStream.Fases(7).SPMProperties.massfraction.GetValueOrDefault * SS

                            S = SM

                        case FlashSpec.H

                            Throw New Exception(DTL.App.GetLocalString("PropPack_FlashTHNotSupported"))

                        case FlashSpec.S

                            Throw New Exception(DTL.App.GetLocalString("PropPack_FlashTSNotSupported"))

                        case FlashSpec.VAP

                            Dim KI(n) As Double
                            Dim HM, HV, HL, HL2 As Double
                            Dim SM, SV, SL, SL2 As Double

                            i = 0
                            Do
                                KI(i) = 0
                                i = i + 1
                            Loop Until i = n + 1

                            T = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature.GetValueOrDefault
                            P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure.GetValueOrDefault

                            Dim Vx, Vx2, Vy As Double()

                            If this.AUX_IS_SINGLECOMP(Fase.Mixture) Then

                                Dim Psat As Double
                                Dim vz As Object = this.RET_VMOL(Fase.Mixture)

                                Psat = this.AUX_PVAPM(T)

                                HL = this.DW_CalcEnthalpy(vz, T, Psat, State.Liquid)
                                HV = this.DW_CalcEnthalpy(vz, T, Psat, State.Vapor)
                                SL = this.DW_CalcEntropy(vz, T, Psat, State.Liquid)
                                SV = this.DW_CalcEntropy(vz, T, Psat, State.Vapor)
                                H = xv * HV + (1 - xv) * HL
                                S = xv * SV + (1 - xv) * SL
                                P = Psat
                                xl = 1 - xv
                                xl2 = 0.0#

                                Vx = vz
                                Vy = vz
                                Vx2 = vz

                            Else

                                result = this.FlashBase.Flash_TV(RET_VMOL(Fase.Mixture), T, xv, P, Me)

                                P = result(4)

                                xl = result(0)
                                xv = result(1)
                                xl2 = result(7)

                                Vx = result(2)
                                Vy = result(3)
                                Vx2 = result(8)

                            End If

                            this.CurrentMaterialStream.Fases(3).SPMProperties.molarfraction = xl
                            this.CurrentMaterialStream.Fases(4).SPMProperties.molarfraction = xl2
                            this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction = xv

                            Dim FCL = this.DW_CalcFugCoeff(Vx, T, P, State.Liquid)
                            Dim FCL2 = this.DW_CalcFugCoeff(Vx2, T, P, State.Liquid)
                            Dim FCV = this.DW_CalcFugCoeff(Vy, T, P, State.Vapor)

                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                subst.FracaoMolar = Vx(i)
                                subst.FugacityCoeff = FCL(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 3)
                            Next
                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                subst.FracaoMolar = Vx2(i)
                                subst.FugacityCoeff = FCL2(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 4)
                            Next
                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                subst.FracaoMolar = Vy(i)
                                subst.FugacityCoeff = FCV(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 2)
                            Next

                            this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction = xl * this.AUX_MMM(Fase.Liquid1) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor))
                            this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction = xl2 * this.AUX_MMM(Fase.Liquid2) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor))
                            this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction = xv * this.AUX_MMM(Fase.Vapor) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor))

                            If xl <> 0 Then HL = this.DW_CalcEnthalpy(Vx, T, P, State.Liquid)
                            If xl2 <> 0 Then HL2 = this.DW_CalcEnthalpy(Vx2, T, P, State.Liquid)
                            If xv <> 0 Then HV = this.DW_CalcEnthalpy(Vy, T, P, State.Vapor)
                            HM = this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction.GetValueOrDefault * HL2 + this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction.GetValueOrDefault * HL + this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault * HV

                            H = HM


                            If xl <> 0 Then SL = this.DW_CalcEntropy(Vx, T, P, State.Liquid)
                            If xl2 <> 0 Then SL2 = this.DW_CalcEntropy(Vx2, T, P, State.Liquid)
                            If xv <> 0 Then SV = this.DW_CalcEntropy(Vy, T, P, State.Vapor)
                            SM = this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction.GetValueOrDefault * SL2 + this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction.GetValueOrDefault * SL + this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault * SV

                            S = SM

                    End Select

                case FlashSpec.P

                     switch spec2

                        case FlashSpec.H

                            T = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature.GetValueOrDefault
                            P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure.GetValueOrDefault

                            If Double.IsNaN(H) Or Double.IsInfinity(H) Then H = this.CurrentMaterialStream.Fases(0).SPMProperties.molar_enthalpy.GetValueOrDefault / this.CurrentMaterialStream.Fases(0).SPMProperties.molecularWeight.GetValueOrDefault

                            If this.AUX_IS_SINGLECOMP(Fase.Mixture) Then

                                Dim brentsolverT As New BrentOpt.Brent
                                brentsolverT.DefineFuncDelegate(AddressOf EnthalpyTx)

                                Dim hl, hv, sl, sv, Tsat As Double
                                Dim vz As Object = this.RET_VMOL(Fase.Mixture)

                                P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure.GetValueOrDefault

                                Tsat = 0.0#
                                For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                                    Tsat += subst.FracaoMolar * this.AUX_TSATi(P, subst.Nome)
                                Next

                                hl = this.DW_CalcEnthalpy(vz, Tsat, P, State.Liquid)
                                hv = this.DW_CalcEnthalpy(vz, Tsat, P, State.Vapor)
                                sl = this.DW_CalcEntropy(vz, Tsat, P, State.Liquid)
                                sv = this.DW_CalcEntropy(vz, Tsat, P, State.Vapor)
                                If H <= hl Then
                                    xv = 0
                                    LoopVarState = State.Liquid
                                ElseIf H >= hv Then
                                    xv = 1
                                    LoopVarState = State.Vapor
                                Else
                                    xv = (H - hl) / (hv - hl)
                                End If
                                S = xv * sv + (1 - xv) * sl

                                If xv <> 0.0# And xv <> 1.0# Then
                                    T = Tsat
                                Else
                                    LoopVarF = H
                                    LoopVarX = P
                                    T = brentsolverT.BrentOpt(273.15, 623.15, 20, 0.0001, 1000, Nothing)
                                End If
                                xl = 1 - xv

                                If T <= this.AUX_TFM(Fase.Mixture) Then

                                    'solid only.

                                    xv = 0.0#
                                    xl = 0.0#
                                    xs = 1.0#

                                    Dim constprops As New List(Of ConstantProperties)
                                    For Each su As Substancia In this.CurrentMaterialStream.Fases(0).Componentes.Values
                                        constprops.Add(su.ConstantProperties)
                                    Next

                                    S = this.DW_CalcSolidEnthalpy(T, vz, constprops) / (T - 298.15)

                                    this.CurrentMaterialStream.Fases(3).SPMProperties.molarfraction = xl
                                    this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction = xv
                                    this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction = xl
                                    this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction = xv
                                    this.CurrentMaterialStream.Fases(7).SPMProperties.massfraction = xs
                                    this.CurrentMaterialStream.Fases(7).SPMProperties.molarfraction = xs
                                    this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction = 0.0#
                                    this.CurrentMaterialStream.Fases(4).SPMProperties.molarfraction = 0.0#

                                    i = 0
                                    For Each subst In this.CurrentMaterialStream.Fases(7).Componentes.Values
                                        subst.FracaoMolar = vz(i)
                                        subst.FugacityCoeff = 1
                                        subst.ActivityCoeff = 1
                                        subst.PartialVolume = 0
                                        subst.PartialPressure = P
                                        subst.FracaoMassica = vz(i)
                                        i += 1
                                    Next

                                Else

                                    this.CurrentMaterialStream.Fases(3).SPMProperties.molarfraction = xl
                                    this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction = xv
                                    this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction = xl
                                    this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction = xv

                                    i = 0
                                    For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                        subst.FracaoMolar = vz(i)
                                        subst.FugacityCoeff = 1
                                        subst.ActivityCoeff = 1
                                        subst.PartialVolume = 0
                                        subst.PartialPressure = P
                                        subst.FracaoMassica = vz(i)
                                        i += 1
                                    Next
                                    i = 0
                                    For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                        subst.FracaoMolar = vz(i)
                                        subst.FugacityCoeff = 1
                                        subst.ActivityCoeff = 1
                                        subst.PartialVolume = 0
                                        subst.PartialPressure = P
                                        subst.FracaoMassica = vz(i)
                                        i += 1
                                    Next

                                End If

                            Else

redirect:                       result = this.FlashBase.Flash_PH(RET_VMOL(Fase.Mixture), P, H, T, Me)

                                T = result(4)

                                xl = result(0)
                                xv = result(1)
                                xl2 = result(7)
                                xs = result(9)

                                this.CurrentMaterialStream.Fases(3).SPMProperties.molarfraction = xl
                                this.CurrentMaterialStream.Fases(4).SPMProperties.molarfraction = xl2
                                this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction = xv
                                this.CurrentMaterialStream.Fases(7).SPMProperties.molarfraction = xs

                                Dim Vx = result(2)
                                Dim Vy = result(3)
                                Dim Vx2 = result(8)
                                Dim Vs = result(10)

                                Dim FCL = this.DW_CalcFugCoeff(Vx, T, P, State.Liquid)
                                Dim FCL2 = this.DW_CalcFugCoeff(Vx2, T, P, State.Liquid)
                                Dim FCV = this.DW_CalcFugCoeff(Vy, T, P, State.Vapor)
                                Dim FCS = this.DW_CalcFugCoeff(Vs, T, P, State.Solid)

                                i = 0
                                For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                    subst.FracaoMolar = Vx(i)
                                    subst.FugacityCoeff = FCL(i)
                                    subst.ActivityCoeff = 0
                                    subst.PartialVolume = 0
                                    subst.PartialPressure = 0
                                    i += 1
                                Next
                                i = 1
                                For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                    subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 3)
                                    i += 1
                                Next
                                i = 0
                                For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                    subst.FracaoMolar = Vx2(i)
                                    subst.FugacityCoeff = FCL2(i)
                                    subst.ActivityCoeff = 0
                                    subst.PartialVolume = 0
                                    subst.PartialPressure = 0
                                    i += 1
                                Next
                                i = 1
                                For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                    subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 4)
                                    i += 1
                                Next
                                i = 0
                                For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                    subst.FracaoMolar = Vy(i)
                                    subst.FugacityCoeff = FCV(i)
                                    subst.ActivityCoeff = 0
                                    subst.PartialVolume = 0
                                    subst.PartialPressure = 0
                                    i += 1
                                Next
                                i = 1
                                For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                    subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 2)
                                    i += 1
                                Next
                                i = 0
                                For Each subst In this.CurrentMaterialStream.Fases(7).Componentes.Values
                                    subst.FracaoMolar = Vs(i)
                                    subst.FugacityCoeff = FCS(i)
                                    subst.ActivityCoeff = 0
                                    subst.PartialVolume = 0
                                    subst.PartialPressure = 0
                                    i += 1
                                Next
                                For Each subst In this.CurrentMaterialStream.Fases(7).Componentes.Values
                                    subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 7)
                                Next

                                this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction = xl * this.AUX_MMM(Fase.Liquid1) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor) + xs * this.AUX_MMM(Fase.Solid))
                                this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction = xl2 * this.AUX_MMM(Fase.Liquid2) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor) + xs * this.AUX_MMM(Fase.Solid))
                                this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction = xv * this.AUX_MMM(Fase.Vapor) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor) + xs * this.AUX_MMM(Fase.Solid))
                                this.CurrentMaterialStream.Fases(7).SPMProperties.massfraction = xs * this.AUX_MMM(Fase.Solid) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor) + xs * this.AUX_MMM(Fase.Solid))

                                Dim constprops As New List(Of ConstantProperties)
                                For Each su As Substancia In this.CurrentMaterialStream.Fases(0).Componentes.Values
                                    constprops.Add(su.ConstantProperties)
                                Next

                                Dim SM, SV, SL, SL2, SS As Double

                                If xl <> 0 Then SL = this.DW_CalcEntropy(Vx, T, P, State.Liquid)
                                If xl2 <> 0 Then SL2 = this.DW_CalcEntropy(Vx2, T, P, State.Liquid)
                                If xv <> 0 Then SV = this.DW_CalcEntropy(Vy, T, P, State.Vapor)
                                If xs <> 0 Then SS = this.DW_CalcSolidEnthalpy(T, Vs, constprops) / (T - 298.15)
                                SM = this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction.GetValueOrDefault * SL2 + this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction.GetValueOrDefault * SL + this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault * SV + this.CurrentMaterialStream.Fases(7).SPMProperties.massfraction.GetValueOrDefault * SS

                                S = SM

                            End If

                        case FlashSpec.S

                            T = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature.GetValueOrDefault
                            P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure.GetValueOrDefault

                            If Double.IsNaN(S) Or Double.IsInfinity(S) Then S = this.CurrentMaterialStream.Fases(0).SPMProperties.molar_entropy.GetValueOrDefault / this.CurrentMaterialStream.Fases(0).SPMProperties.molecularWeight.GetValueOrDefault

                            If this.AUX_IS_SINGLECOMP(Fase.Mixture) And this.ComponentName <> "FPROPS" Then

                                Dim brentsolverT As New BrentOpt.Brent
                                brentsolverT.DefineFuncDelegate(AddressOf EnthalpyTx)

                                Dim hl, hv, sl, sv, Tsat As Double
                                Dim vz As Object = this.RET_VMOL(Fase.Mixture)

                                P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure.GetValueOrDefault

                                Tsat = 0.0#
                                For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                                    Tsat += subst.FracaoMolar * this.AUX_TSATi(P, subst.Nome)
                                Next

                                hl = this.DW_CalcEnthalpy(vz, Tsat, P, State.Liquid)
                                hv = this.DW_CalcEnthalpy(vz, Tsat, P, State.Vapor)
                                sl = this.DW_CalcEntropy(vz, Tsat, P, State.Liquid)
                                sv = this.DW_CalcEntropy(vz, Tsat, P, State.Vapor)
                                If S <= sl Then
                                    xv = 0
                                    LoopVarState = State.Liquid
                                ElseIf S >= sv Then
                                    xv = 1
                                    LoopVarState = State.Vapor
                                Else
                                    xv = (S - sl) / (sv - sl)
                                End If
                                H = xv * hv + (1 - xv) * hl

                                If xv <> 0.0# And xv <> 1.0# Then
                                    T = Tsat
                                Else
                                    LoopVarF = H
                                    LoopVarX = P
                                    T = brentsolverT.BrentOpt(273.15, 623.15, 20, 0.0001, 1000, Nothing)
                                End If
                                xl = 1 - xv

                                this.CurrentMaterialStream.Fases(3).SPMProperties.molarfraction = xl
                                this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction = xv
                                this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction = xl
                                this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction = xv

                                i = 0
                                For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                    subst.FracaoMolar = vz(i)
                                    subst.FugacityCoeff = 1
                                    subst.ActivityCoeff = 1
                                    subst.PartialVolume = 0
                                    subst.PartialPressure = P
                                    subst.FracaoMassica = vz(i)
                                    i += 1
                                Next
                                i = 0
                                For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                    subst.FracaoMolar = vz(i)
                                    subst.FugacityCoeff = 1
                                    subst.ActivityCoeff = 1
                                    subst.PartialVolume = 0
                                    subst.PartialPressure = P
                                    subst.FracaoMassica = vz(i)
                                    i += 1
                                Next

                            Else

redirect2:                      result = this.FlashBase.Flash_PS(RET_VMOL(Fase.Mixture), P, S, T, Me)

                                T = result(4)

                                xl = result(0)
                                xv = result(1)
                                xl2 = result(7)

                                this.CurrentMaterialStream.Fases(3).SPMProperties.molarfraction = xl
                                this.CurrentMaterialStream.Fases(4).SPMProperties.molarfraction = xl2
                                this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction = xv

                                Dim Vx = result(2)
                                Dim Vy = result(3)
                                Dim Vx2 = result(8)

                                Dim FCL = this.DW_CalcFugCoeff(Vx, T, P, State.Liquid)
                                Dim FCL2 = this.DW_CalcFugCoeff(Vx2, T, P, State.Liquid)
                                Dim FCV = this.DW_CalcFugCoeff(Vy, T, P, State.Vapor)

                                i = 0
                                For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                    subst.FracaoMolar = Vx(i)
                                    subst.FugacityCoeff = FCL(i)
                                    subst.ActivityCoeff = 0
                                    subst.PartialVolume = 0
                                    subst.PartialPressure = 0
                                    i += 1
                                Next
                                i = 1
                                For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                    subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 3)
                                    i += 1
                                Next
                                i = 0
                                For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                    subst.FracaoMolar = Vx2(i)
                                    subst.FugacityCoeff = FCL2(i)
                                    subst.ActivityCoeff = 0
                                    subst.PartialVolume = 0
                                    subst.PartialPressure = 0
                                    i += 1
                                Next
                                i = 1
                                For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                    subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 4)
                                    i += 1
                                Next
                                i = 0
                                For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                    subst.FracaoMolar = Vy(i)
                                    subst.FugacityCoeff = FCV(i)
                                    subst.ActivityCoeff = 0
                                    subst.PartialVolume = 0
                                    subst.PartialPressure = 0
                                    i += 1
                                Next

                                i = 1
                                For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                    subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 2)
                                    i += 1
                                Next

                                this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction = xl * this.AUX_MMM(Fase.Liquid1) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor))
                                this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction = xl2 * this.AUX_MMM(Fase.Liquid2) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor))
                                this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction = xv * this.AUX_MMM(Fase.Vapor) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor))

                                Dim HM, HV, HL, HL2 As Double

                                If xl <> 0 Then HL = this.DW_CalcEnthalpy(Vx, T, P, State.Liquid)
                                If xl2 <> 0 Then HL2 = this.DW_CalcEnthalpy(Vx2, T, P, State.Liquid)
                                If xv <> 0 Then HV = this.DW_CalcEnthalpy(Vy, T, P, State.Vapor)
                                HM = this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction.GetValueOrDefault * HL2 + this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction.GetValueOrDefault * HL + this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault * HV

                                H = HM

                            End If

                        case FlashSpec.VAP

                            Dim KI(n) As Double
                            Dim HM, HV, HL, HL2 As Double
                            Dim SM, SV, SL, SL2 As Double

                            i = 0
                            Do
                                KI(i) = 0
                                i = i + 1
                            Loop Until i = n + 1

                            T = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature.GetValueOrDefault
                            P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure.GetValueOrDefault

                            Dim Vx, Vx2, Vy As Double()

                            If this.AUX_IS_SINGLECOMP(Fase.Mixture) Then

                                Dim Tsat As Double
                                Dim vz As Object = this.RET_VMOL(Fase.Mixture)

                                Tsat = 0.0#
                                For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                                    Tsat += subst.FracaoMolar * this.AUX_TSATi(P, subst.Nome)
                                Next

                                HL = this.DW_CalcEnthalpy(vz, Tsat, P, State.Liquid)
                                HV = this.DW_CalcEnthalpy(vz, Tsat, P, State.Vapor)
                                SL = this.DW_CalcEntropy(vz, Tsat, P, State.Liquid)
                                SV = this.DW_CalcEntropy(vz, Tsat, P, State.Vapor)
                                H = xv * HV + (1 - xv) * HL
                                S = xv * SV + (1 - xv) * SL
                                T = Tsat
                                xl = 1 - xv
                                xl2 = 0.0#

                                Vx = vz
                                Vy = vz
                                Vx2 = vz

                            Else

                                result = this.FlashBase.Flash_PV(RET_VMOL(Fase.Mixture), P, xv, T, Me)

                                T = result(4)

                                xl = result(0)
                                xv = result(1)
                                xl2 = result(7)

                                Vx = result(2)
                                Vy = result(3)
                                Vx2 = result(8)

                            End If

                            this.CurrentMaterialStream.Fases(3).SPMProperties.molarfraction = xl
                            this.CurrentMaterialStream.Fases(4).SPMProperties.molarfraction = xl2
                            this.CurrentMaterialStream.Fases(2).SPMProperties.molarfraction = xv

                            Dim FCL = this.DW_CalcFugCoeff(Vx, T, P, State.Liquid)
                            Dim FCL2 = this.DW_CalcFugCoeff(Vx2, T, P, State.Liquid)
                            Dim FCV = this.DW_CalcFugCoeff(Vy, T, P, State.Vapor)

                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                subst.FracaoMolar = Vx(i)
                                subst.FugacityCoeff = FCL(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 3)
                            Next
                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                subst.FracaoMolar = Vx2(i)
                                subst.FugacityCoeff = FCL2(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 4)
                            Next
                            i = 0
                            For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                subst.FracaoMolar = Vy(i)
                                subst.FugacityCoeff = FCV(i)
                                subst.ActivityCoeff = 0
                                subst.PartialVolume = 0
                                subst.PartialPressure = 0
                                i += 1
                            Next
                            For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                                subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, 2)
                            Next

                            this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction = xl * this.AUX_MMM(Fase.Liquid1) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor))
                            this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction = xl2 * this.AUX_MMM(Fase.Liquid2) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor))
                            this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction = xv * this.AUX_MMM(Fase.Vapor) / (xl * this.AUX_MMM(Fase.Liquid1) + xl2 * this.AUX_MMM(Fase.Liquid2) + xv * this.AUX_MMM(Fase.Vapor))

                            If xl <> 0 Then HL = this.DW_CalcEnthalpy(Vx, T, P, State.Liquid)
                            If xl2 <> 0 Then HL2 = this.DW_CalcEnthalpy(Vx2, T, P, State.Liquid)
                            If xv <> 0 Then HV = this.DW_CalcEnthalpy(Vy, T, P, State.Vapor)
                            HM = this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction.GetValueOrDefault * HL2 + this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction.GetValueOrDefault * HL + this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault * HV

                            H = HM


                            If xl <> 0 Then SL = this.DW_CalcEntropy(Vx, T, P, State.Liquid)
                            If xl2 <> 0 Then SL2 = this.DW_CalcEntropy(Vx2, T, P, State.Liquid)
                            If xv <> 0 Then SV = this.DW_CalcEntropy(Vy, T, P, State.Vapor)
                            SM = this.CurrentMaterialStream.Fases(4).SPMProperties.massfraction.GetValueOrDefault * SL2 + this.CurrentMaterialStream.Fases(3).SPMProperties.massfraction.GetValueOrDefault * SL + this.CurrentMaterialStream.Fases(2).SPMProperties.massfraction.GetValueOrDefault * SV

                            S = SM

                    End Select

            End Select

            Dim summf As Double = 0, sumwf As Double = 0
            For Each pi As PhaseInfo In this.PhaseMappings.Values
                If Not pi.PhaseLabel = "Disabled" Then
                    summf += this.CurrentMaterialStream.Fases(pi.DWPhaseIndex).SPMProperties.molarfraction.GetValueOrDefault
                    sumwf += this.CurrentMaterialStream.Fases(pi.DWPhaseIndex).SPMProperties.massfraction.GetValueOrDefault
                End If
            Next
            If Abs(summf - 1) > 0.000001 Then
                For Each pi As PhaseInfo In this.PhaseMappings.Values
                    If Not pi.PhaseLabel = "Disabled" Then
                        If Not this.CurrentMaterialStream.Fases(pi.DWPhaseIndex).SPMProperties.molarfraction.HasValue Then
                            this.CurrentMaterialStream.Fases(pi.DWPhaseIndex).SPMProperties.molarfraction = 1 - summf
                            this.CurrentMaterialStream.Fases(pi.DWPhaseIndex).SPMProperties.massfraction = 1 - sumwf
                        End If
                    End If
                Next
            End If

            With this.CurrentMaterialStream

                .Fases(0).SPMProperties.temperature = T
                .Fases(0).SPMProperties.pressure = P
                .Fases(0).SPMProperties.enthalpy = H
                .Fases(0).SPMProperties.entropy = S

            End With

            this.CurrentMaterialStream.AtEquilibrium = True
        */
    }

        private double EnthalpyTx(double x, object otherargs )
        {
            double er = LoopVarF - this.DW_CalcEnthalpy(this.RET_VMOL(Fase.Mixture), x, LoopVarX, LoopVarState);
            return er;
        }

        private double  EnthalpyPx(double x, object otherargs)
        {
            double er = LoopVarF - this.DW_CalcEnthalpy(this.RET_VMOL(Fase.Mixture), LoopVarX, x, LoopVarState);
            return er;
        }

        public abstract void DW_CalcVazaoMolar();

        public abstract void DW_CalcVazaoMassica();

        public abstract void DW_CalcVazaoVolumetrica();

        public abstract double DW_CalcMassaEspecifica_ISOL(Fase fase1 , double T, double P, double  Pvp  = 0) ;

        public abstract double DW_CalcViscosidadeDinamica_ISOL(Fase fase1 , double T, double P);


        public abstract double DW_CalcEnergiaMistura_ISOL(double T, double P);

        public abstract double DW_CalcCp_ISOL(Fase fase1 , double T, double P);

        public abstract double DW_CalcCv_ISOL(Fase fase1 , double T, double P);

        public abstract double DW_CalcK_ISOL(Fase fase1 , double T, double P) ;

        public abstract double DW_CalcMM_ISOL(Fase fase1 , double T, double P);

        public abstract double DW_CalcPVAP_ISOL(double T);

        public abstract void DW_CalcCompPartialVolume(Fase phase, double T, double P);



        public virtual void DW_CalcKvalue()
        {
            //modif adel
        /*
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each phase As DTL.ClassesBasicasTermodinamica.Fase In this.CurrentMaterialStream.Fases.Values
                For Each subst In phase.Componentes.Values
                    subst.Kvalue = this.CurrentMaterialStream.Fases(2).Componentes(subst.Nome).FracaoMolar.GetValueOrDefault / phase.Componentes(subst.Nome).FracaoMolar.GetValueOrDefault
                    subst.lnKvalue = Log(subst.Kvalue)
                Next
            Next*/
         }

        public void DW_CalcCompMolarFlow(int phaseID )
        {   
            //modif adel
                /*
            If Not phaseID = -1 Then
                With this.CurrentMaterialStream.Fases(phaseID)
                    For Each subs As Substancia In .Componentes.Values
                        subs.MolarFlow = .SPMProperties.molarflow.GetValueOrDefault * subs.FracaoMolar.GetValueOrDefault
                    Next
                End With
            Else
                For Each phase As DTL.ClassesBasicasTermodinamica.Fase In this.CurrentMaterialStream.Fases.Values
                    With phase
                        For Each subs As Substancia In .Componentes.Values
                            subs.MolarFlow = .SPMProperties.molarflow.GetValueOrDefault * subs.FracaoMolar.GetValueOrDefault
                        Next
                    End With
                Next
            End If*/
            }


        public void DW_CalcCompFugCoeff(Fase f )
            {
//modif adel
/*
            Dim fc As Object
            Dim vmol As Object = this.RET_VMOL(f)
            Dim P, T As Double
            P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure.GetValueOrDefault
            T = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature.GetValueOrDefault
             switch f
                case Fase.Vapor
                    fc = this.DW_CalcFugCoeff(vmol, T, P, State.Vapor)
                case Else
                    fc = this.DW_CalcFugCoeff(vmol, T, P, State.Liquid)
            End Select
            Dim i As Integer = 0
            For Each subs As Substancia In this.CurrentMaterialStream.Fases(this.RET_PHASEID(f)).Componentes.Values
                subs.FugacityCoeff = fc(i)
                subs.ActivityCoeff = fc(i) * P / this.AUX_PVAPi(i, T)
                i += 1
            Next*/
    }

        public void DW_CalcCompMassFlow(int phaseID)
            {
            
            //modif adel
            /*
            If Not phaseID = -1 Then
                With this.CurrentMaterialStream.Fases(phaseID)
                    For Each subs As Substancia In .Componentes.Values
                        subs.MassFlow = .SPMProperties.massflow.GetValueOrDefault * subs.FracaoMassica.GetValueOrDefault
                    Next
                End With
            Else
                For Each phase As DTL.ClassesBasicasTermodinamica.Fase In this.CurrentMaterialStream.Fases.Values
                    With phase
                        For Each subs As Substancia In .Componentes.Values
                            subs.MassFlow = .SPMProperties.massflow.GetValueOrDefault * subs.FracaoMassica.GetValueOrDefault
                        Next
                    End With
                Next
            End If*/
        }

        public void DW_CalcCompVolFlow(int phaseID)
        {
            //modif adel
            /*
            Dim TotalMolarFlow, TotalVolFlow, MolarFrac, PartialVol, VolFrac, VolFlow, Sum As Double

            Dim T, P As Double
            T = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature.GetValueOrDefault
            P = this.CurrentMaterialStream.Fases(0).SPMProperties.pressure.GetValueOrDefault
            this.DW_CalcCompPartialVolume(this.RET_PHASECODE(phaseID), T, P)

            If Not phaseID = -1 Then
                With this.CurrentMaterialStream.Fases(phaseID)

                    Sum = 0

                    For Each subs As Substancia In .Componentes.Values
                        TotalMolarFlow = .SPMProperties.molarflow.GetValueOrDefault
                        TotalVolFlow = .SPMProperties.volumetric_flow.GetValueOrDefault
                        MolarFrac = subs.FracaoMolar.GetValueOrDefault
                        PartialVol = subs.PartialVolume.GetValueOrDefault
                        VolFlow = TotalMolarFlow * MolarFrac * PartialVol
                        If TotalVolFlow > 0 Then
                            VolFrac = VolFlow / TotalVolFlow
                        Else
                            VolFrac = 0
                        End If
                        subs.VolumetricFraction = VolFrac
                        Sum += VolFrac
                    Next

                    'Normalization is still needed due to minor deviations in the partial molar volume estimation. Summation of partial flow rates
                    'should match phase flow rate.
                    For Each subs As Substancia In .Componentes.Values
                        If Sum > 0 Then
                            subs.VolumetricFraction = subs.VolumetricFraction.GetValueOrDefault / Sum
                        Else
                            subs.VolumetricFraction = 0
                        End If
                        'Corrects volumetric flow rate after normalization of fractions.
                        subs.VolumetricFlow = subs.VolumetricFraction.GetValueOrDefault * TotalVolFlow
                    Next

                End With
            Else
                For Each phase As DTL.ClassesBasicasTermodinamica.Fase In this.CurrentMaterialStream.Fases.Values
                    With phase

                        Sum = 0

                        For Each subs As Substancia In .Componentes.Values
                            TotalMolarFlow = .SPMProperties.molarflow.GetValueOrDefault
                            TotalVolFlow = .SPMProperties.volumetric_flow.GetValueOrDefault
                            MolarFrac = subs.FracaoMolar.GetValueOrDefault
                            PartialVol = subs.PartialVolume.GetValueOrDefault
                            VolFlow = TotalMolarFlow * MolarFrac * PartialVol
                            If TotalVolFlow > 0 Then
                                VolFrac = VolFlow / TotalVolFlow
                            Else
                                VolFrac = 0
                            End If
                            subs.VolumetricFraction = VolFrac
                            Sum += VolFrac
                        Next

                        'Normalization is still needed due to minor deviations in the partial molar volume estimation. Summation of partial flow rates
                        'should match phase flow rate.
                        For Each subs As Substancia In .Componentes.Values
                            If Sum > 0 Then
                                subs.VolumetricFraction = subs.VolumetricFraction.GetValueOrDefault / Sum
                            Else
                                subs.VolumetricFraction = 0
                            End If
                            'Corrects volumetric flow rate after normalization of fractions.
                            subs.VolumetricFlow = subs.VolumetricFraction.GetValueOrDefault * TotalVolFlow
                        Next

                    End With
                Next
            End If

            'Totalization for the mixture "phase" should be made separatly, since the concept o partial molar volume is non sense for the whole mixture.
            For Each Subs As Substancia In CurrentMaterialStream.Fases(0).Componentes.Values
                Subs.VolumetricFraction = 0
                Subs.VolumetricFlow = 0
            Next

            'Summation of volumetric flow rates over all phases.
            For APhaseID As Integer = 1 To this.CurrentMaterialStream.Fases.Count - 1
                For Each Subs As String In CurrentMaterialStream.Fases(APhaseID).Componentes.Keys
                    CurrentMaterialStream.Fases(0).Componentes(Subs).VolumetricFlow = CurrentMaterialStream.Fases(0).Componentes(Subs).VolumetricFlow.GetValueOrDefault + CurrentMaterialStream.Fases(APhaseID).Componentes(Subs).VolumetricFlow.GetValueOrDefault
                Next
            Next

            'Calculate volumetric fractions for the mixture.
            For Each Subs As Substancia In CurrentMaterialStream.Fases(0).Componentes.Values
                Subs.VolumetricFraction = Subs.VolumetricFlow.GetValueOrDefault / CurrentMaterialStream.Fases(0).SPMProperties.volumetric_flow.GetValueOrDefault
            Next
            */
        }

        public void DW_ZerarPhaseProps(Fase fase)
            {
                //mdofi adel
            /*
            Dim phaseID As Integer

            phaseID = this.RET_PHASEID(fase)

            If this.CurrentMaterialStream.Fases.ContainsKey(phaseID) Then

                If phaseID <> 0 Then

                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.density =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.enthalpy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.entropy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_enthalpy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_entropy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.enthalpyF =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.entropyF =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_enthalpyF =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_entropyF =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.compressibilityFactor =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.heatCapacityCp =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.heatCapacityCv =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molecularWeight =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.thermalConductivity =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.speedOfSound =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.volumetric_flow =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.jouleThomsonCoefficient =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.excessEnthalpy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.excessEntropy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.compressibility =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.bubbleTemperature =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.bubblePressure =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.dewTemperature =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.dewPressure =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.viscosity =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.kinematic_viscosity =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molarflow =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.massflow =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.massfraction =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molarfraction =null;

                Else

                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.density =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.enthalpy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.entropy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_enthalpy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_entropy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.enthalpyF =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.entropyF =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_enthalpyF =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molar_entropyF =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.compressibilityFactor =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.heatCapacityCp =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.heatCapacityCv =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.molecularWeight =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.thermalConductivity =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.speedOfSound =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.volumetric_flow =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.jouleThomsonCoefficient =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.excessEnthalpy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.excessEntropy =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.compressibility =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.bubbleTemperature =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.bubblePressure =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.dewTemperature =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.dewPressure =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.viscosity =null;
                    this.CurrentMaterialStream.Fases(phaseID).SPMProperties.kinematic_viscosity =null;

                End If

            End If
            */
            }

        public void DW_ZerarTwoPhaseProps(Fase fase1, Fase fase2)
        {
            this.CurrentMaterialStream.Fases(0).TPMProperties.kvalue =null;
            this.CurrentMaterialStream.Fases(0).TPMProperties.logKvalue =null;
            this.CurrentMaterialStream.Fases(0).TPMProperties.surfaceTension =null;
        }
        

        public void DW_ZerarOverallProps()
            {}           


        public void DW_ZerarVazaoMolar()
        {
            this.CurrentMaterialStream.Fases(0).SPMProperties.molarflow = 0
        }

        public void DW_ZerarVazaoVolumetrica()
        {
            this.CurrentMaterialStream.Fases(0).SPMProperties.volumetric_flow = 0
        }

        public void DW_ZerarVazaoMassica()
        {
            this.CurrentMaterialStream.Fases(0).SPMProperties.massflow = 0
        }

        public void DW_ZerarComposicoes(Fase fase)
        {
            //modif adel
            /*
            Dim phaseID As Integer
            phaseID = this.RET_PHASEID(fase)

            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(phaseID).Componentes.Values
                subst.FracaoMolar =null;
                subst.FracaoMassica =null;
            Next*/
        }

        public double AUX_TCM(Fase fase)
        {
            return 0.0;
            // modif adel
            /*
            Dim Tc As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                Tc += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.Critical_Temperature
            Next

            return Tc*/
        }

        public double AUX_TBM(Fase fase)
        {
            return 0.0;

            //modif adel
            /*
            Dim Tb As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                Tb += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.Normal_Boiling_Point
            Next

            return Tb*/
        }
        

        public double AUX_TFM(Fase fase) 
         {
            return 0.0;

            //modif adel
            /*
            Dim Tf As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                Tf += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.TemperatureOfFusion
            Next

            return Tf*/
        }

        public double AUX_WM(Fase fase)
        {
            //modif adel;
            return 0.0;
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                val += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.Acentric_Factor
            Next

            return val*/
        }

        public Double AUX_ZCM(Fase fase) 
        {
            //modif adel
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                val += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.Critical_Compressibility
            Next

            return val*/
    }

        public double AUX_ZRAM(Fase fase) 
        {    
            //modif adel
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                If subst.ConstantProperties.Z_Rackett <> 0 Then
                    val += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.Z_Rackett
                Else
                    val += subst.FracaoMolar.GetValueOrDefault * (0.29056 - 0.08775 * subst.ConstantProperties.Acentric_Factor)
                End If
            Next

            return val*/
        }

        public double AUX_VCM(Fase fase)
            {
                //modif adel
            /*
            Dim val, vc As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                vc = subst.ConstantProperties.Critical_Volume
                If vc = 0.0# Then
                    vc = m_props.Vc(subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor)
                End If
                val += subst.FracaoMolar.GetValueOrDefault * vc
            Next

            return val / 1000*/
            }

        public double AUX_PCM(Fase fase) 
         {
            //modif adel
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                val += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.Critical_Pressure
            Next

            return val*/
            }

        public double AUX_PVAPM(double T)
          {  
            //modif adel
            /*
            Dim val As Double = 0
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val += subst.FracaoMolar.GetValueOrDefault * this.AUX_PVAPi(subst.Nome, T)
            Next

            return val*/
        }

        public double AUX_KIJ(string sub1, string sub2) 
        {
            //modif adel
            /*
            Dim Vc1 As Double = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Volume
            Dim Vc2 As Double = this.CurrentMaterialStream.Fases(0).Componentes(sub2).ConstantProperties.Critical_Volume

            Dim tmp As Double = 1 - 8 * (Vc1 * Vc2) ^ 0.5 / ((Vc1 ^ (1 / 3) + Vc2 ^ (1 / 3)) ^ 3)

            return tmp*/
        }

        public double AUX_MMM(Fase fase)
            {/*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                val += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.Molar_Weight
            Next

            return val*/
            }

        private double AUX_Rackett_PHIi(string sub1, Fase fase) 
        {
            /*
            Dim val, vc As Double
            vc = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Volume
            If vc = 0.0# Then vc = m_props.Vc(this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes(sub1).ConstantProperties.Critical_Temperature, this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes(sub1).ConstantProperties.Critical_Pressure, this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes(sub1).ConstantProperties.Acentric_Factor)

            val = this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes(sub1).FracaoMolar.GetValueOrDefault * vc

            val = val / this.AUX_VCM(fase) / 1000

            return val*/
            }

        private double AUX_Rackett_Kij(string sub1, string sub2) 
        {
            /*
            Dim Vc1 As Double = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Volume
            Dim Vc2 As Double = this.CurrentMaterialStream.Fases(0).Componentes(sub2).ConstantProperties.Critical_Volume

            If Vc1 = 0.0# Then Vc1 = m_props.Vc(this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Temperature, this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Pressure, this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Acentric_Factor)
            If Vc2 = 0.0# Then Vc2 = m_props.Vc(this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Temperature, this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Pressure, this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Acentric_Factor)

            Dim tmp As Double = 8 * (Vc1 * Vc2) ^ 0.5 / ((Vc1 ^ (1 / 3) + Vc2 ^ (1 / 3)) ^ 3)

            return tmp*/
            }

        private double AUX_Rackett_Tcij(string sub1, string sub2) 
        {
            //modif adel
            /*
            Dim Tc1 As Double = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Temperature
            Dim Tc2 As Double = this.CurrentMaterialStream.Fases(0).Componentes(sub2).ConstantProperties.Critical_Temperature

            Dim tmp As Double = this.AUX_Rackett_Kij(sub1, sub2) * (Tc1 * Tc2) ^ 0.5

            return tmp*/
        }

        private double AUX_Rackett_Tcm(Fase fase) 
        {
            //modif adel
            /*
            Dim Tc As Double
            Dim subst1, subst2 As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst1 In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                For Each subst2 In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                    Tc += this.AUX_Rackett_PHIi(subst1.Nome, fase) * this.AUX_Rackett_PHIi(subst2.Nome, fase) * this.AUX_Rackett_Tcij(subst1.Nome, subst2.Nome)
                Next
            Next

            return Tc*/
            }
 
        public double  AUX_CPi(string sub1, double T)
            {
                //return adel
            /*
            If this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.IsPF = 1 Then

                With this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties

                    return this.m_props.Cpig_lk(.PF_Watson_K, .Acentric_Factor, T) '* .Molar_Weight

                End With

            Else

                If this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "DWSIM" Or _
                this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "" Then
                    Dim A, B, C, D, E, result As Double
                    A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_E
                    'Cp = A + B*T + C*T^2 + D*T^3 + E*T^4 where Cp in kJ/kg-mol , T in K 
                    result = A + B * T + C * T ^ 2 + D * T ^ 3 + E * T ^ 4
                    return result / this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Molar_Weight
                ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "CheResources" Then
                    Dim A, B, C, D, E, result As Double
                    A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_E
                    'CAL/MOL.K [CP=A+(B*T)+(C*T^2)+(D*T^3)], T in K
                    result = A + B * T + C * T ^ 2 + D * T ^ 3
                    return result / this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Molar_Weight * 4.1868
                ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "ChemSep" Or _
                this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "User" Then
                    Dim A, B, C, D, E, result As Double
                    Dim eqno As String = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.IdealgasCpEquation
                    Dim mw As Double = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Molar_Weight
                    A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_E
                    '<rppc name="Ideal gas heat capacity (RPP)"  units="J/kmol/K" >
                    result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) / 1000 / mw 'kJ/kg.K
                    return result
                ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "Biodiesel" Then
                    Dim A, B, C, D, E, result As Double
                    Dim eqno As String = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.IdealgasCpEquation
                    A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_E
                    result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) 'kJ/kg.K
                    return result
                Else
                    return 0
                End If

            End If
            */
        }

        public double AUX_CPm(Fase fase, double T)
        {
            //modif adel
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                val += subst.FracaoMassica.GetValueOrDefault * this.AUX_CPi(subst.Nome, T)
            Next

            return val*/
        }
        

        public virtual double AUX_HFm25(Fase fase)
        {
            //modif adel
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                val += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.IG_Enthalpy_of_Formation_25C * subst.ConstantProperties.Molar_Weight
            Next

            Dim mw As Double = this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).SPMProperties.molecularWeight.GetValueOrDefault

            If mw <> 0.0# Then return val / mw Else return 0.0#*/
        }

        public virtual double AUX_SFm25(Fase fase)
        {
            //modif adel
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                val += subst.FracaoMolar.GetValueOrDefault * subst.ConstantProperties.IG_Entropy_of_Formation_25C * subst.ConstantProperties.Molar_Weight
            Next

            Dim mw As Double = this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).SPMProperties.molecularWeight.GetValueOrDefault

            If mw <> 0.0# Then return val / mw Else return 0.0#*/
        }

        public double AUX_PVAPi(string sub1, double T)
        {
                /*
            If this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.IsPF = 1 Then

                With this.CurrentMaterialStream.Fases(0).Componentes(sub1)

                    return this.m_props.Pvp_leekesler(T, .ConstantProperties.Critical_Temperature, .ConstantProperties.Critical_Pressure, .ConstantProperties.Acentric_Factor)

                End With

            Else


                If this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "DWSIM" Or _
                this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "" Then
                    Dim A, B, C, D, E, result As Double
                    A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_E
                    result = Math.Exp(A + B / T + C * Math.Log(T) + D * T ^ E)
                    return result
                ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "CheResources" Then
                    Dim A, B, C, result As Double
                    A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_C
                    '[LN(P)=A-B/(T+C), P(mmHG) T(K)]
                    result = Math.Exp(A - B / (T + C)) * 133.322368 'mmHg to Pascal
                    return result
                ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "ChemSep" Or _
                this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "User" Then
                    Dim A, B, C, D, E, result As Double
                    Dim eqno As String = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.VaporPressureEquation
                    Dim mw As Double = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Molar_Weight
                    A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_E
                    '<vp_c name="Vapour pressure"  units="Pa" >
                    result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) 'Pa
                    If eqno = "0" Then
                        With this.CurrentMaterialStream.Fases(0).Componentes(sub1)
                            result = this.m_props.Pvp_leekesler(T, .ConstantProperties.Critical_Temperature, .ConstantProperties.Critical_Pressure, .ConstantProperties.Acentric_Factor)
                        End With
                    End If
                    return result
                ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "Biodiesel" Then
                    Dim A, B, C, D, E, result As Double
                    Dim eqno As String = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.VaporPressureEquation
                    A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Vapor_Pressure_Constant_E
                    result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) 'kPa
                    return result * 1000
                Else
                    return 0
                End If

            End If*/
        }

        public void AUX_PVAPi(int index , double T)
        {
            /*
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim nome As String = ""
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                If i = index Then nome = subst.Nome
                i += 1
            Next
            If this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.IsPF = 1 Then
                With this.CurrentMaterialStream.Fases(0).Componentes(nome)
                    return this.m_props.Pvp_leekesler(T, .ConstantProperties.Critical_Temperature, .ConstantProperties.Critical_Pressure, .ConstantProperties.Acentric_Factor)
                End With
            Else
                If this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.OriginalDB = "DWSIM" Or _
                this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.OriginalDB = "" Then
                    Dim A, B, C, D, E, result As Double
                    A = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_E
                    result = Math.Exp(A + B / T + C * Math.Log(T) + D * T ^ E)
                    return result
                ElseIf this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.OriginalDB = "CheResources" Then
                    Dim A, B, C, result As Double
                    A = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_C
                    '[LN(P)=A-B/(T+C), P(mmHG) T(K)]
                    result = Math.Exp(A - B / (T + C)) * 133.322368 'mmHg to Pascal
                    return result
                ElseIf this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.OriginalDB = "ChemSep" Or _
                this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.OriginalDB = "User" Then
                    Dim A, B, C, D, E, result As Double
                    Dim eqno As String = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.VaporPressureEquation
                    Dim mw As Double = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Molar_Weight
                    A = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_E
                    '<vp_c name="Vapour pressure"  units="Pa" >
                    result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) 'Pa.s
                    If eqno = "0" Then
                        With this.CurrentMaterialStream.Fases(0).Componentes(nome)
                            result = this.m_props.Pvp_leekesler(T, .ConstantProperties.Critical_Temperature, .ConstantProperties.Critical_Pressure, .ConstantProperties.Acentric_Factor)
                        End With
                    End If
                    return result
                ElseIf this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.OriginalDB = "Biodiesel" Then
                    Dim A, B, C, D, E, result As Double
                    Dim eqno As String = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.VaporPressureEquation
                    A = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_A
                    B = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_B
                    C = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_C
                    D = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_D
                    E = this.CurrentMaterialStream.Fases(0).Componentes(nome).ConstantProperties.Vapor_Pressure_Constant_E
                    result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) 'kPa
                    return result * 1000
                Else
                    return 0
                End If
            End If*/
        }

        public double AUX_TSATi(double PVAP , string subst) 
        {
            /*
            Dim i As Integer

            Dim Tinf, Tsup As Double

            Dim fT, fT_inf, nsub, delta_T As Double

            Tinf = 100
            Tsup = 2000

            nsub = 10

            delta_T = (Tsup - Tinf) / nsub

            i = 0
            Do
                i = i + 1
                fT = PVAP - this.AUX_PVAPi(subst, Tinf)
                Tinf = Tinf + delta_T
                fT_inf = PVAP - this.AUX_PVAPi(subst, Tinf)
            Loop Until fT * fT_inf < 0 Or fT_inf > fT Or i >= 100
            Tsup = Tinf
            Tinf = Tinf - delta_T

            'método de Brent para encontrar Vc

            Dim aaa, bbb, ccc, ddd, eee, min11, min22, faa, fbb, fcc, ppp, qqq, rrr, sss, tol11, xmm As Double
            Dim ITMAX2 As Integer = 100
            Dim iter2 As Integer

            aaa = Tinf
            bbb = Tsup
            ccc = Tsup

            faa = PVAP - this.AUX_PVAPi(subst, aaa)
            fbb = PVAP - this.AUX_PVAPi(subst, bbb)
            fcc = fbb

            iter2 = 0
            Do
                If (fbb > 0 And fcc > 0) Or (fbb < 0 And fcc < 0) Then
                    ccc = aaa
                    fcc = faa
                    ddd = bbb - aaa
                    eee = ddd
                End If
                If Math.Abs(fcc) < Math.Abs(fbb) Then
                    aaa = bbb
                    bbb = ccc
                    ccc = aaa
                    faa = fbb
                    fbb = fcc
                    fcc = faa
                End If
                tol11 = 0.0001
                xmm = 0.5 * (ccc - bbb)
                If (Math.Abs(xmm) <= tol11) Or (fbb = 0) Then GoTo Final3
                If (Math.Abs(eee) >= tol11) And (Math.Abs(faa) > Math.Abs(fbb)) Then
                    sss = fbb / faa
                    If aaa = ccc Then
                        ppp = 2 * xmm * sss
                        qqq = 1 - sss
                    Else
                        qqq = faa / fcc
                        rrr = fbb / fcc
                        ppp = sss * (2 * xmm * qqq * (qqq - rrr) - (bbb - aaa) * (rrr - 1))
                        qqq = (qqq - 1) * (rrr - 1) * (sss - 1)
                    End If
                    If ppp > 0 Then qqq = -qqq
                    ppp = Math.Abs(ppp)
                    min11 = 3 * xmm * qqq - Math.Abs(tol11 * qqq)
                    min22 = Math.Abs(eee * qqq)
                    Dim tvar2 As Double
                    If min11 < min22 Then tvar2 = min11
                    If min11 > min22 Then tvar2 = min22
                    If 2 * ppp < tvar2 Then
                        eee = ddd
                        ddd = ppp / qqq
                    Else
                        ddd = xmm
                        eee = ddd
                    End If
                Else
                    ddd = xmm
                    eee = ddd
                End If
                aaa = bbb
                faa = fbb
                If (Math.Abs(ddd) > tol11) Then
                    bbb += ddd
                Else
                    bbb += Math.Sign(xmm) * tol11
                End If
                fbb = PVAP - this.AUX_PVAPi(subst, bbb)
                iter2 += 1
            Loop Until iter2 = ITMAX2

Final3:

            return bbb*/
        }

        public double AUX_TSATi(double PVAP, int index)
        {
           /*
            Dim i As Integer

            Dim Tinf, Tsup As Double

            Dim fT, fT_inf, nsub, delta_T As Double

            Tinf = 100
            Tsup = 2000

            nsub = 10

            delta_T = (Tsup - Tinf) / nsub

            i = 0
            Do
                i = i + 1
                fT = PVAP - this.AUX_PVAPi(index, Tinf)
                Tinf = Tinf + delta_T
                fT_inf = PVAP - this.AUX_PVAPi(index, Tinf)
            Loop Until fT * fT_inf < 0 Or fT_inf > fT Or i >= 100
            Tsup = Tinf
            Tinf = Tinf - delta_T

            'método de Brent para encontrar Vc

            Dim aaa, bbb, ccc, ddd, eee, min11, min22, faa, fbb, fcc, ppp, qqq, rrr, sss, tol11, xmm As Double
            Dim ITMAX2 As Integer = 100
            Dim iter2 As Integer

            aaa = Tinf
            bbb = Tsup
            ccc = Tsup

            faa = PVAP - this.AUX_PVAPi(index, aaa)
            fbb = PVAP - this.AUX_PVAPi(index, bbb)
            fcc = fbb

            iter2 = 0
            Do
                If (fbb > 0 And fcc > 0) Or (fbb < 0 And fcc < 0) Then
                    ccc = aaa
                    fcc = faa
                    ddd = bbb - aaa
                    eee = ddd
                End If
                If Math.Abs(fcc) < Math.Abs(fbb) Then
                    aaa = bbb
                    bbb = ccc
                    ccc = aaa
                    faa = fbb
                    fbb = fcc
                    fcc = faa
                End If
                tol11 = 0.0001
                xmm = 0.5 * (ccc - bbb)
                If (Math.Abs(xmm) <= tol11) Or (fbb = 0) Then GoTo Final3
                If (Math.Abs(eee) >= tol11) And (Math.Abs(faa) > Math.Abs(fbb)) Then
                    sss = fbb / faa
                    If aaa = ccc Then
                        ppp = 2 * xmm * sss
                        qqq = 1 - sss
                    Else
                        qqq = faa / fcc
                        rrr = fbb / fcc
                        ppp = sss * (2 * xmm * qqq * (qqq - rrr) - (bbb - aaa) * (rrr - 1))
                        qqq = (qqq - 1) * (rrr - 1) * (sss - 1)
                    End If
                    If ppp > 0 Then qqq = -qqq
                    ppp = Math.Abs(ppp)
                    min11 = 3 * xmm * qqq - Math.Abs(tol11 * qqq)
                    min22 = Math.Abs(eee * qqq)
                    Dim tvar2 As Double
                    If min11 < min22 Then tvar2 = min11
                    If min11 > min22 Then tvar2 = min22
                    If 2 * ppp < tvar2 Then
                        eee = ddd
                        ddd = ppp / qqq
                    Else
                        ddd = xmm
                        eee = ddd
                    End If
                Else
                    ddd = xmm
                    eee = ddd
                End If
                aaa = bbb
                faa = fbb
                If (Math.Abs(ddd) > tol11) Then
                    bbb += ddd
                Else
                    bbb += Math.Sign(xmm) * tol11
                End If
                fbb = PVAP - this.AUX_PVAPi(index, bbb)
                iter2 += 1
            Loop Until iter2 = ITMAX2

Final3:

            return bbb*/
            return 0.0;
        }

        public double AUX_HVAPi(string sub1, double T)
        {
            /*
            Dim A, B, C, D, Tr, result As Double
            A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.HVap_A
            B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.HVap_B
            C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.HVap_C
            D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.HVap_D

            Tr = T / this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Temperature

            If this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "DWSIM" Or _
                                this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "" Then
                If this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.IsHYPO = 1 Or _
                this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.IsPF = 1 Then
                    Dim tr1 As Double
                    tr1 = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Normal_Boiling_Point / this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Temperature
                    result = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.HVap_A * ((1 - Tr) / (1 - tr1)) ^ 0.375
                    return result 'kJ/kg
                Else
                    result = A * (1 - Tr) ^ (B + C * Tr + D * Tr ^ 2)
                    return result / this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Molar_Weight / 1000 'kJ/kg
                End If
            ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "CheResources" Or _
            this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "User" Then
                Dim tr1 As Double
                tr1 = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Normal_Boiling_Point / this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Temperature
                result = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.HVap_A * ((1 - Tr) / (1 - tr1)) ^ 0.375
                return result 'kJ/kg
            ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "ChemSep" Then
                Dim eqno As String = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.VaporizationEnthalpyEquation
                result = this.CalcCSTDepProp(eqno, A, B, C, D, 0, T, T / Tr) / this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Molar_Weight / 1000 'kJ/kg
                return result
            Else
                return 0
            End If*/
        }

        public double AUX_LIQVISCi(string sub1, double T)
        {
                /*
            If T / this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Temperature < 1 Then

                If this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.IsPF = 1 Then

                    With this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties

                        return this.m_props.oilvisc_twu(T, .PF_Tv1, .PF_Tv2, .PF_v1, .PF_v2) * this.m_props.liq_dens_rackett(T, .Critical_Temperature, .Critical_Pressure, .Acentric_Factor, .Molar_Weight, .Z_Rackett)

                    End With

                Else
                    If this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "DWSIM" Or _
                    this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "" Then
                        Dim A, B, C, D, E, result As Double
                        A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_A
                        B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_B
                        C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_C
                        D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_D
                        E = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_E
                        result = Math.Exp(A + B / T + C * Math.Log(T) + D * T ^ E)
                        return result
                    ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "CheResources" Then
                        Dim B, C, result As Double
                        B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_B
                        C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_C
                        '[LOG(V)=B*(1/T-1/C), T(K) V(CP)]
                        result = Exp(B * (1 / T - 1 / C)) * 0.001
                        return result
                    ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "ChemSep" Or _
                    this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "User" Then
                        Dim A, B, C, D, E, result As Double
                        Dim eqno As String = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.LiquidViscosityEquation
                        Dim mw As Double = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Molar_Weight
                        A = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_A
                        B = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_B
                        C = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_C
                        D = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_D
                        E = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Liquid_Viscosity_Const_E
                        '<lvsc name="Liquid viscosity"  units="Pa.s" >
                        result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) 'Pa.s
                        If eqno = "0" Or eqno = "" Then
                            Dim Tc, Pc, w As Double
                            Tc = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Temperature
                            Pc = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Pressure
                            w = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Acentric_Factor
                            mw = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Molar_Weight
                            result = this.m_props.viscl_letsti(T, Tc, Pc, w, mw)
                        End If
                        return result
                    ElseIf this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.OriginalDB = "Biodiesel" Then
                        Dim result As Double
                        Dim Tc, Pc, w, Mw As Double
                        Tc = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Temperature
                        Pc = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Critical_Pressure
                        w = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Acentric_Factor
                        Mw = this.CurrentMaterialStream.Fases(0).Componentes(sub1).ConstantProperties.Molar_Weight
                        result = this.m_props.viscl_letsti(T, Tc, Pc, w, Mw)
                        return result
                    Else
                        return 0
                    End If

                End If

            Else

                return 0.0#

            End If*/
        }

        public double AUX_LIQTHERMCONDi(ConstantProperties cprop, double T) 
        {
        
            /*Dim val As Double

            If cprop.LiquidHeatCapacityEquation <> "" And cprop.LiquidHeatCapacityEquation <> "0" And Not cprop.IsIon And Not cprop.IsSalt Then
                val = this.CalcCSTDepProp(cprop.LiquidThermalConductivityEquation, cprop.Liquid_Thermal_Conductivity_Const_A, cprop.Liquid_Thermal_Conductivity_Const_B, cprop.Liquid_Thermal_Conductivity_Const_C, cprop.Liquid_Thermal_Conductivity_Const_D, cprop.Liquid_Thermal_Conductivity_Const_E, T, cprop.Critical_Temperature)
            ElseIf cprop.IsIon Or cprop.IsSalt Then
                val = 0.0#
            Else
                val = this.m_props.condl_latini(T, cprop.Normal_Boiling_Point, cprop.Critical_Temperature, cprop.Molar_Weight, "")
            End If

            return val*/
          }

        public double AUX_VAPTHERMCONDi(ConstantProperties cprop , double T, double P)
        {
            /*
            Dim val As Double = this.m_props.condtg_elyhanley(T, cprop.Critical_Temperature, cprop.Critical_Volume / 1000, cprop.Critical_Compressibility, cprop.Acentric_Factor, cprop.Molar_Weight, this.AUX_CPi(cprop.Name, T) * cprop.Molar_Weight - 8.314)

            return val*/
            return 0.0;
        }
    
        public double AUX_LIQVISCm(double T, int phaseid  = 3) 
        {  
            /* 
            Dim val, val2, logvisc, result As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            val = 0
            val2 = 0
            For Each subst In this.CurrentMaterialStream.Fases(phaseid).Componentes.Values
                'logvisc = Math.Log(this.AUX_LIQVISCi(subst.Nome, T))
                If Not Double.IsInfinity(logvisc) Then
                    val += subst.FracaoMolar.GetValueOrDefault * this.AUX_LIQVISCi(subst.Nome, T)
                Else
                    val2 += subst.FracaoMolar.GetValueOrDefault
                End If
            Next

            result = (val / (1 - val2))

            return result*/
            return 0.0;
        }

        public double AUX_VAPVISCi(ConstantProperties cprop , double T) 
        {
            /*
            Dim val As Double

            val = this.m_props.viscg_lucas(T, cprop.Critical_Temperature, cprop.Critical_Pressure, cprop.Acentric_Factor, cprop.Molar_Weight)

            return val*/
         }

        public virtual double AUX_SOLIDDENS() 
        {
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim zerodens As Double = 0
            Dim db As String
            Dim T As Double = this.CurrentMaterialStream.Fases(0).SPMProperties.temperature.GetValueOrDefault

            For Each subst In this.CurrentMaterialStream.Fases(7).Componentes.Values
                db = subst.ConstantProperties.OriginalDB
                If db = "ChemSep" Or (db = "User" And subst.ConstantProperties.SolidDensityEquation <> "") Then
                    Dim A, B, C, D, E, result As Double
                    Dim eqno As String = subst.ConstantProperties.SolidDensityEquation
                    Dim mw As Double = subst.ConstantProperties.Molar_Weight
                    A = subst.ConstantProperties.Solid_Density_Const_A
                    B = subst.ConstantProperties.Solid_Density_Const_B
                    C = subst.ConstantProperties.Solid_Density_Const_C
                    D = subst.ConstantProperties.Solid_Density_Const_D
                    E = subst.ConstantProperties.Solid_Density_Const_E
                    result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) 'kmol/m3
                    val += subst.FracaoMassica.GetValueOrDefault * 1 / (result * mw)
                Else
                    If subst.ConstantProperties.SolidDensityAtTs <> 0.0# Then
                        val += subst.FracaoMassica.GetValueOrDefault * 1 / subst.ConstantProperties.SolidDensityAtTs
                    Else
                        zerodens += subst.FracaoMassica.GetValueOrDefault
                    End If
                End If
            Next

                return 1 / val / (1 - zerodens)*/
        }


        public double  AUX_SOLIDDENSi(ConstantProperties cprop , double T ) 
         {
            /*   
            Dim val As Double
            Dim zerodens As Double = 0

            If cprop.OriginalDB = "ChemSep" Or (cprop.OriginalDB = "User" And cprop.SolidDensityEquation <> "") Then
                Dim A, B, C, D, E, result As Double
                Dim eqno As String = cprop.SolidDensityEquation
                Dim mw As Double = cprop.Molar_Weight
                A = cprop.Solid_Density_Const_A
                B = cprop.Solid_Density_Const_B
                C = cprop.Solid_Density_Const_C
                D = cprop.Solid_Density_Const_D
                E = cprop.Solid_Density_Const_E
                result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) 'kmol/m3
                val = 1 / (result * mw)
            Else
                If cprop.SolidDensityAtTs <> 0.0# Then
                    val = 1 / cprop.SolidDensityAtTs
                Else
                    val = 1.0E+20
                End If
            End If

            return 1 / val
            */
        }

        public virtual double AUX_SolidHeatCapacity(ConstantProperties cprop , double T)
        { 
            /*
            Dim val As Double

            If cprop.OriginalDB = "ChemSep" Or (cprop.OriginalDB = "User" And cprop.SolidDensityEquation <> "") Then
                Dim A, B, C, D, E, result As Double
                Dim eqno As String = cprop.SolidHeatCapacityEquation
                Dim mw As Double = cprop.Molar_Weight
                A = cprop.Solid_Heat_Capacity_Const_A
                B = cprop.Solid_Heat_Capacity_Const_B
                C = cprop.Solid_Heat_Capacity_Const_C
                D = cprop.Solid_Heat_Capacity_Const_D
                E = cprop.Solid_Heat_Capacity_Const_E
                result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) 'J/kmol/K
                val = result / 1000 / mw 'kJ/kg.K
            Else
                val = 3 ' replacement if no params available
            End If

            return val
            */  
        }

        public double AUX_SOLIDCP(Array Vxm , List<ConstantProperties> cprops, double T) 
        {
            /*
            Dim n As Integer = UBound(Vxm)
            Dim val As Double = 0
            For i As Integer = 0 To n
                val += Vxm(i) * AUX_SolidHeatCapacity(cprops(i), T)
            Next
            return val*/
        }

        public double AUX_SURFTM(double T)
        {
            /*
            If m_props Is Nothing Then m_props = New DTL.SimulationObjects.PropertyPackages.Auxiliary.PROPS

            Dim val As Double = 0
            Dim nbp As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim ftotal As Double = 1

            For Each subst In this.CurrentMaterialStream.Fases(1).Componentes.Values
                If T / subst.ConstantProperties.Critical_Temperature < 1 Then
                    nbp = subst.ConstantProperties.Normal_Boiling_Point
                    If nbp = 0 Then nbp = 0.7 * subst.ConstantProperties.Critical_Temperature
                    subst.TDProperties.surfaceTension = this.m_props.sigma_bb(T, nbp, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure)
                Else
                    subst.TDProperties.surfaceTension = 0
                    ftotal -= subst.FracaoMolar.GetValueOrDefault
                End If
                val += subst.FracaoMolar.GetValueOrDefault * subst.TDProperties.surfaceTension.GetValueOrDefault / ftotal
            Next

            return val*/
        }

        public double AUX_CONDTL(double T, int phaseid  = 3) 
        {
            /*
            If m_props Is Nothing Then m_props = New DTL.SimulationObjects.PropertyPackages.Auxiliary.PROPS

            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim vcl(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1)
            Dim i As Integer = 0
            For Each subst In this.CurrentMaterialStream.Fases(phaseid).Componentes.Values
                If this.Parameters.ContainsKey("PP_USEEXPLIQTHERMALCOND") Then
                    If CInt(this.Parameters("PP_USEEXPLIQTHERMALCOND")) = 1 Then
                        If subst.ConstantProperties.LiquidThermalConductivityEquation <> "" Then
                            vcl(i) = this.CalcCSTDepProp(subst.ConstantProperties.LiquidThermalConductivityEquation, subst.ConstantProperties.Liquid_Thermal_Conductivity_Const_A, subst.ConstantProperties.Liquid_Thermal_Conductivity_Const_B, subst.ConstantProperties.Liquid_Thermal_Conductivity_Const_C, subst.ConstantProperties.Liquid_Thermal_Conductivity_Const_D, subst.ConstantProperties.Liquid_Thermal_Conductivity_Const_E, T, subst.ConstantProperties.Critical_Temperature)
                        Else
                            vcl(i) = this.m_props.condl_latini(T, subst.ConstantProperties.Normal_Boiling_Point, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Molar_Weight, "")
                        End If
                    Else
                        vcl(i) = this.m_props.condl_latini(T, subst.ConstantProperties.Normal_Boiling_Point, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Molar_Weight, "")
                    End If
                Else
                    vcl(i) = this.m_props.condl_latini(T, subst.ConstantProperties.Normal_Boiling_Point, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Molar_Weight, "")
                End If
                i = i + 1
            Next
            val = this.m_props.condlm_li(this.RET_VVC, vcl, this.RET_VMOL(this.RET_PHASECODE(phaseid)))
            return val*/
        }

        public Double AUX_CONDTG(double T, double P)
        {
            /*
            If m_props Is Nothing Then m_props = New DTL.SimulationObjects.PropertyPackages.Auxiliary.PROPS

            Dim val As Double = this.m_props.condtg_elyhanley(T, this.AUX_TCM(Fase.Vapor), this.AUX_VCM(Fase.Vapor), this.AUX_ZCM(Fase.Vapor), this.AUX_WM(Fase.Vapor), this.AUX_MMM(Fase.Vapor), this.DW_CalcCv_ISOL(Fase.Vapor, T, P) * this.AUX_MMM(Fase.Vapor))

            return val*/         
        }


        public Double AUX_VAPVISCm(double T, double RHO , double MM ) 
        {
            /*
            If m_props Is Nothing Then m_props = New DTL.SimulationObjects.PropertyPackages.Auxiliary.PROPS

            Dim val As Double

            val = this.m_props.viscg_lucas(T, this.AUX_TCM(Fase.Vapor), this.AUX_PCM(Fase.Vapor), this.AUX_WM(Fase.Vapor), this.AUX_MMM(Fase.Vapor))
            val = this.m_props.viscg_jossi_stiel_thodos(val, T, MM / RHO / 1000, AUX_TCM(Fase.Vapor), AUX_PCM(Fase.Vapor), AUX_VCM(Fase.Vapor), AUX_MMM(Fase.Vapor))

            return val*/
        }

        public virtual Double AUX_LIQDENS(double T, double P = 0, double Pvp = 0, int phaseid = 3, bool FORCE_EOS = false) 
        {
            /*
            If m_props Is Nothing Then m_props = New DTL.SimulationObjects.PropertyPackages.Auxiliary.PROPS

            Dim val As Double
            Dim m_pr2 As New DTL.SimulationObjects.PropertyPackages.Auxiliary.PengRobinson

            If phaseid = 1 Then
                If T / this.AUX_TCM(Fase.Liquid) > 1 Then

                    Dim Z = m_pr2.Z_PR(T, P, RET_VMOL(Fase.Liquid), RET_VKij(), RET_VTC, RET_VPC, RET_VW, "L")
                    val = 1 / (8.314 * Z * T / P)
                    val = val * this.AUX_MMM(Fase.Liquid) / 1000

                Else

                    If this.Parameters.ContainsKey("PP_IDEAL_MIXRULE_LIQDENS") Then
                        If CInt(this.Parameters("PP_IDEAL_MIXRULE_LIQDENS")) = 1 Then
                            Dim i As Integer
                            Dim vk(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
                            i = 0
                            For Each subst As Substancia In this.CurrentMaterialStream.Fases(phaseid).Componentes.Values
                                If this.Parameters.ContainsKey("PP_USEEXPLIQDENS") Then
                                    If CInt(this.Parameters("PP_USEEXPLIQDENS")) = 1 Then
                                        If subst.ConstantProperties.LiquidDensityEquation <> "" And subst.ConstantProperties.LiquidDensityEquation <> "0" Then
                                            vk(i) = this.CalcCSTDepProp(subst.ConstantProperties.LiquidDensityEquation, subst.ConstantProperties.Liquid_Density_Const_A, subst.ConstantProperties.Liquid_Density_Const_B, subst.ConstantProperties.Liquid_Density_Const_C, subst.ConstantProperties.Liquid_Density_Const_D, subst.ConstantProperties.Liquid_Density_Const_E, T, subst.ConstantProperties.Critical_Temperature)
                                            vk(i) = subst.ConstantProperties.Molar_Weight * vk(i)
                                        Else
                                            vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                        End If
                                    Else
                                        vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                    End If
                                Else
                                    vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                End If
                                vk(i) = subst.FracaoMassica / vk(i)
                                i = i + 1
                            Next
                            val = 1 / MathEx.Common.Sum(vk)
                        Else
                            val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Liquid), this.AUX_PCM(Fase.Liquid), this.AUX_WM(Fase.Liquid), this.AUX_MMM(Fase.Liquid), this.AUX_ZRAM(Fase.Liquid), P, this.AUX_PVAPM(T))
                        End If
                    Else
                        val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Liquid), this.AUX_PCM(Fase.Liquid), this.AUX_WM(Fase.Liquid), this.AUX_MMM(Fase.Liquid), this.AUX_ZRAM(Fase.Liquid), P, this.AUX_PVAPM(T))
                    End If

                End If

            ElseIf phaseid = 2 Then

                If T / this.AUX_TCM(Fase.Vapor) > 1 Then

                    Dim Z = m_pr2.Z_PR(T, P, RET_VMOL(Fase.Vapor), RET_VKij(), RET_VTC, RET_VPC, RET_VW, "L")
                    val = 1 / (8.314 * Z * T / P)
                    val = val * this.AUX_MMM(Fase.Vapor) / 1000

                Else

                    Dim vk(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
                    val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Vapor), this.AUX_PCM(Fase.Vapor), this.AUX_WM(Fase.Vapor), this.AUX_MMM(Fase.Vapor), this.AUX_ZRAM(Fase.Vapor), P, this.AUX_PVAPM(T))

                End If
            ElseIf phaseid = 3 Then
                If T / this.AUX_TCM(Fase.Liquid1) > 1 Then

                    Dim Z = m_pr2.Z_PR(T, P, RET_VMOL(Fase.Liquid1), RET_VKij(), RET_VTC, RET_VPC, RET_VW, "L")
                    val = 1 / (8.314 * Z * T / P)
                    val = val * this.AUX_MMM(Fase.Liquid1) / 1000

                Else

                    If this.Parameters.ContainsKey("PP_IDEAL_MIXRULE_LIQDENS") Then
                        If CInt(this.Parameters("PP_IDEAL_MIXRULE_LIQDENS")) = 1 Then
                            Dim i As Integer
                            Dim vk(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
                            i = 0
                            For Each subst As Substancia In this.CurrentMaterialStream.Fases(phaseid).Componentes.Values
                                If this.Parameters.ContainsKey("PP_USEEXPLIQDENS") Then
                                    If CInt(this.Parameters("PP_USEEXPLIQDENS")) = 1 Then
                                        If subst.ConstantProperties.LiquidDensityEquation <> "" And subst.ConstantProperties.LiquidDensityEquation <> "0" Then
                                            vk(i) = this.CalcCSTDepProp(subst.ConstantProperties.LiquidDensityEquation, subst.ConstantProperties.Liquid_Density_Const_A, subst.ConstantProperties.Liquid_Density_Const_B, subst.ConstantProperties.Liquid_Density_Const_C, subst.ConstantProperties.Liquid_Density_Const_D, subst.ConstantProperties.Liquid_Density_Const_E, T, subst.ConstantProperties.Critical_Temperature)
                                            vk(i) = subst.ConstantProperties.Molar_Weight * vk(i)
                                        Else
                                            vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                        End If
                                    Else
                                        vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                    End If
                                Else
                                    vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                End If
                                vk(i) = subst.FracaoMassica / vk(i)
                                i = i + 1
                            Next
                            val = 1 / MathEx.Common.Sum(vk)
                        Else
                            val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Liquid1), this.AUX_PCM(Fase.Liquid1), this.AUX_WM(Fase.Liquid1), this.AUX_MMM(Fase.Liquid1), this.AUX_ZRAM(Fase.Liquid1), P, this.AUX_PVAPM(T))
                        End If
                    Else
                        val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Liquid1), this.AUX_PCM(Fase.Liquid1), this.AUX_WM(Fase.Liquid1), this.AUX_MMM(Fase.Liquid1), this.AUX_ZRAM(Fase.Liquid1), P, this.AUX_PVAPM(T))
                    End If

                End If
            ElseIf phaseid = 4 Then
                If T / this.AUX_TCM(Fase.Liquid2) > 1 Then

                    Dim Z = m_pr2.Z_PR(T, P, RET_VMOL(Fase.Liquid2), RET_VKij(), RET_VTC, RET_VPC, RET_VW, "L")
                    val = 1 / (8.314 * Z * T / P)
                    val = val * this.AUX_MMM(Fase.Liquid2) / 1000

                Else

                    If this.Parameters.ContainsKey("PP_IDEAL_MIXRULE_LIQDENS") Then
                        If CInt(this.Parameters("PP_IDEAL_MIXRULE_LIQDENS")) = 1 Then
                            Dim i As Integer
                            Dim vk(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
                            i = 0
                            For Each subst As Substancia In this.CurrentMaterialStream.Fases(phaseid).Componentes.Values
                                If this.Parameters.ContainsKey("PP_USEEXPLIQDENS") Then
                                    If CInt(this.Parameters("PP_USEEXPLIQDENS")) = 1 Then
                                        If subst.ConstantProperties.LiquidDensityEquation <> "" And subst.ConstantProperties.LiquidDensityEquation <> "0" Then
                                            vk(i) = this.CalcCSTDepProp(subst.ConstantProperties.LiquidDensityEquation, subst.ConstantProperties.Liquid_Density_Const_A, subst.ConstantProperties.Liquid_Density_Const_B, subst.ConstantProperties.Liquid_Density_Const_C, subst.ConstantProperties.Liquid_Density_Const_D, subst.ConstantProperties.Liquid_Density_Const_E, T, subst.ConstantProperties.Critical_Temperature)
                                            vk(i) = subst.ConstantProperties.Molar_Weight * vk(i)
                                        Else
                                            vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                        End If
                                    Else
                                        vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                    End If
                                Else
                                    vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                End If
                                vk(i) = subst.FracaoMassica / vk(i)
                                i = i + 1
                            Next
                            val = 1 / MathEx.Common.Sum(vk)
                        Else
                            val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Liquid2), this.AUX_PCM(Fase.Liquid2), this.AUX_WM(Fase.Liquid2), this.AUX_MMM(Fase.Liquid2), this.AUX_ZRAM(Fase.Liquid2), P, this.AUX_PVAPM(T))
                        End If
                    Else
                        val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Liquid2), this.AUX_PCM(Fase.Liquid2), this.AUX_WM(Fase.Liquid2), this.AUX_MMM(Fase.Liquid2), this.AUX_ZRAM(Fase.Liquid2), P, this.AUX_PVAPM(T))
                    End If

                End If
            ElseIf phaseid = 5 Then
                If T / this.AUX_TCM(Fase.Liquid3) > 1 Then

                    Dim Z = m_pr2.Z_PR(T, P, RET_VMOL(Fase.Liquid3), RET_VKij(), RET_VTC, RET_VPC, RET_VW, "L")
                    val = 1 / (8.314 * Z * T / P)
                    val = val * this.AUX_MMM(Fase.Liquid3) / 1000

                Else

                    If this.Parameters.ContainsKey("PP_IDEAL_MIXRULE_LIQDENS") Then
                        If CInt(this.Parameters("PP_IDEAL_MIXRULE_LIQDENS")) = 1 Then
                            Dim i As Integer
                            Dim vk(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
                            i = 0
                            For Each subst As Substancia In this.CurrentMaterialStream.Fases(phaseid).Componentes.Values
                                If this.Parameters.ContainsKey("PP_USEEXPLIQDENS") Then
                                    If CInt(this.Parameters("PP_USEEXPLIQDENS")) = 1 Then
                                        If subst.ConstantProperties.LiquidDensityEquation <> "" And subst.ConstantProperties.LiquidDensityEquation <> "0" Then
                                            vk(i) = this.CalcCSTDepProp(subst.ConstantProperties.LiquidDensityEquation, subst.ConstantProperties.Liquid_Density_Const_A, subst.ConstantProperties.Liquid_Density_Const_B, subst.ConstantProperties.Liquid_Density_Const_C, subst.ConstantProperties.Liquid_Density_Const_D, subst.ConstantProperties.Liquid_Density_Const_E, T, subst.ConstantProperties.Critical_Temperature)
                                            vk(i) = subst.ConstantProperties.Molar_Weight * vk(i)
                                        Else
                                            vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                        End If
                                    Else
                                        vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                    End If
                                Else
                                    vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                End If
                                vk(i) = subst.FracaoMassica / vk(i)
                                i = i + 1
                            Next
                            val = 1 / MathEx.Common.Sum(vk)
                        Else
                            val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Liquid3), this.AUX_PCM(Fase.Liquid3), this.AUX_WM(Fase.Liquid3), this.AUX_MMM(Fase.Liquid3), this.AUX_ZRAM(Fase.Liquid3), P, this.AUX_PVAPM(T))
                        End If
                    Else
                        val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Liquid3), this.AUX_PCM(Fase.Liquid3), this.AUX_WM(Fase.Liquid3), this.AUX_MMM(Fase.Liquid3), this.AUX_ZRAM(Fase.Liquid3), P, this.AUX_PVAPM(T))
                    End If

                End If
            ElseIf phaseid = 6 Then
                If T / this.AUX_TCM(Fase.Aqueous) > 1 Then

                    Dim Z = m_pr2.Z_PR(T, P, RET_VMOL(Fase.Aqueous), RET_VKij(), RET_VTC, RET_VPC, RET_VW, "L")
                    val = 1 / (8.314 * Z * T / P)
                    val = val * this.AUX_MMM(Fase.Aqueous) / 1000

                Else

                    If this.Parameters.ContainsKey("PP_IDEAL_MIXRULE_LIQDENS") Then
                        If CInt(this.Parameters("PP_IDEAL_MIXRULE_LIQDENS")) = 1 Then
                            Dim i As Integer
                            Dim vk(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
                            i = 0
                            For Each subst As Substancia In this.CurrentMaterialStream.Fases(phaseid).Componentes.Values
                                If this.Parameters.ContainsKey("PP_USEEXPLIQDENS") Then
                                    If CInt(this.Parameters("PP_USEEXPLIQDENS")) = 1 Then
                                        If subst.ConstantProperties.LiquidDensityEquation <> "" And subst.ConstantProperties.LiquidDensityEquation <> "0" Then
                                            vk(i) = this.CalcCSTDepProp(subst.ConstantProperties.LiquidDensityEquation, subst.ConstantProperties.Liquid_Density_Const_A, subst.ConstantProperties.Liquid_Density_Const_B, subst.ConstantProperties.Liquid_Density_Const_C, subst.ConstantProperties.Liquid_Density_Const_D, subst.ConstantProperties.Liquid_Density_Const_E, T, subst.ConstantProperties.Critical_Temperature)
                                            vk(i) = subst.ConstantProperties.Molar_Weight * vk(i)
                                        Else
                                            vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                        End If
                                    Else
                                        vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                    End If
                                Else
                                    vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                                End If
                                vk(i) = subst.FracaoMassica / vk(i)
                                i = i + 1
                            Next
                            val = 1 / MathEx.Common.Sum(vk)
                        Else
                            val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Aqueous), this.AUX_PCM(Fase.Aqueous), this.AUX_WM(Fase.Aqueous), this.AUX_MMM(Fase.Aqueous), this.AUX_ZRAM(Fase.Aqueous), P, this.AUX_PVAPM(T))
                        End If
                    Else
                        val = this.m_props.liq_dens_rackett(T, this.AUX_Rackett_Tcm(Fase.Aqueous), this.AUX_PCM(Fase.Aqueous), this.AUX_WM(Fase.Aqueous), this.AUX_MMM(Fase.Aqueous), this.AUX_ZRAM(Fase.Aqueous), P, this.AUX_PVAPM(T))
                    End If

                End If
            End If

            m_pr2 =null;

            return val*/
        }

        public virtual double AUX_LIQDENS(double T, Array Vx,  double P = 0,   double Pvp  = 0,  bool FORCE_EOS  = false)
        {   
                /*
            If m_props Is Nothing Then m_props = New DTL.SimulationObjects.PropertyPackages.Auxiliary.PROPS

            Dim val As Double
            Dim m_pr2 As New DTL.SimulationObjects.PropertyPackages.Auxiliary.PengRobinson

            Dim i As Integer
            Dim vk(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            i = 0
            For Each subst As Substancia In this.CurrentMaterialStream.Fases(1).Componentes.Values
                If this.Parameters.ContainsKey("PP_USEEXPLIQDENS") Then
                    If CInt(this.Parameters("PP_USEEXPLIQDENS")) = 1 Then
                        If subst.ConstantProperties.LiquidDensityEquation <> "" And subst.ConstantProperties.LiquidDensityEquation <> "0" And Not subst.ConstantProperties.IsIon And Not subst.ConstantProperties.IsSalt Then
                            vk(i) = this.CalcCSTDepProp(subst.ConstantProperties.LiquidDensityEquation, subst.ConstantProperties.Liquid_Density_Const_A, subst.ConstantProperties.Liquid_Density_Const_B, subst.ConstantProperties.Liquid_Density_Const_C, subst.ConstantProperties.Liquid_Density_Const_D, subst.ConstantProperties.Liquid_Density_Const_E, T, subst.ConstantProperties.Critical_Temperature)
                            vk(i) = subst.ConstantProperties.Molar_Weight * vk(i)
                        Else
                            vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                        End If
                    Else
                        vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                    End If
                Else
                    vk(i) = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, P, this.AUX_PVAPi(subst.Nome, T))
                End If
                If T > subst.ConstantProperties.Critical_Temperature Then
                    vk(i) = 1.0E+20
                End If
                If Not Double.IsNaN(vk(i)) Then vk(i) = Vx(i) / vk(i) Else vk(i) = 0.0#
                i = i + 1
            Next
            val = 1 / MathEx.Common.Sum(vk)

            m_pr2 =null;

            return val 'kg/m3
                */
        }

        public virtual double AUX_LIQDENSi(Substancia subst, double T)
        {
            /*
            If m_props Is Nothing Then m_props = New DTL.SimulationObjects.PropertyPackages.Auxiliary.PROPS

            Dim val As Double

            If subst.ConstantProperties.LiquidDensityEquation <> "" And subst.ConstantProperties.LiquidDensityEquation <> "0" And Not subst.ConstantProperties.IsIon And Not subst.ConstantProperties.IsSalt Then
                val = this.CalcCSTDepProp(subst.ConstantProperties.LiquidDensityEquation, subst.ConstantProperties.Liquid_Density_Const_A, subst.ConstantProperties.Liquid_Density_Const_B, subst.ConstantProperties.Liquid_Density_Const_C, subst.ConstantProperties.Liquid_Density_Const_D, subst.ConstantProperties.Liquid_Density_Const_E, T, subst.ConstantProperties.Critical_Temperature)
                val = subst.ConstantProperties.Molar_Weight * val
            Else
                val = this.m_props.liq_dens_rackett(T, subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor, subst.ConstantProperties.Molar_Weight, subst.ConstantProperties.Z_Rackett, 101325, this.AUX_PVAPi(subst.Nome, T))
            End If

            return val 'kg/m3*/
        }

        public virtual double AUX_LIQDENSi(ConstantProperties cprop , double T) 
        {
            /*
            Dim val As Double

            If cprop.LiquidDensityEquation <> "" And cprop.LiquidDensityEquation <> "0" And Not cprop.IsIon And Not cprop.IsSalt Then
                val = this.CalcCSTDepProp(cprop.LiquidDensityEquation, cprop.Liquid_Density_Const_A, cprop.Liquid_Density_Const_B, cprop.Liquid_Density_Const_C, cprop.Liquid_Density_Const_D, cprop.Liquid_Density_Const_E, T, cprop.Critical_Temperature)
                val = cprop.Molar_Weight * val
            Else
                val = this.m_props.liq_dens_rackett(T, cprop.Critical_Temperature, cprop.Critical_Pressure, cprop.Acentric_Factor, cprop.Molar_Weight, cprop.Z_Rackett, 101325, this.AUX_PVAPi(cprop.Name, T))
            End If

            return val 'kg/m3*/
         }

        public virtual double AUX_LIQ_Cpi(ConstantProperties cprop , double T)
        {
            /*
            Dim val As Double

            If cprop.LiquidHeatCapacityEquation <> "" And cprop.LiquidHeatCapacityEquation <> "0" And Not cprop.IsIon And Not cprop.IsSalt Then
                val = this.CalcCSTDepProp(cprop.LiquidHeatCapacityEquation, cprop.Liquid_Heat_Capacity_Const_A, cprop.Liquid_Heat_Capacity_Const_B, cprop.Liquid_Heat_Capacity_Const_C, cprop.Liquid_Heat_Capacity_Const_D, cprop.Liquid_Heat_Capacity_Const_E, T, cprop.Critical_Temperature)
                val = val / 1000 / cprop.Molar_Weight 'kJ/kg.K
            Else
                'estimate using Rownlinson/Bondi correlation
                val = this.m_props.Cpl_rb(AUX_CPi(cprop.Name, T), T, cprop.Critical_Temperature, cprop.Acentric_Factor, cprop.Molar_Weight) 'kJ/kg.K
            End If

            return val*/
        }

        public abstract Double AUX_VAPDENS(double T, double P);

        public double AUX_INT_CPDTi(double T1, double T2,string subst)
        {
                /*
            Dim A, B, C, D, E, result As Double
            A = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_A
            B = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_B
            C = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_C
            D = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_D
            E = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_E

            result = A * (T2 - T1) + B / 2 * (T2 ^ 2 - T1 ^ 2) + C / 3 * (T2 ^ 3 - T1 ^ 3) + D / 4 * (T2 ^ 4 - T1 ^ 4) + E / 5 * (T2 ^ 5 - T1 ^ 5)

            Dim deltaT As Double = (T2 - T1) / 10
            Dim result2, Ti As Double
            Dim i As Integer = 0
            Dim integral As Double = 0

            Ti = T1 + deltaT
            For i = 0 To 9
                integral += this.AUX_CPi(subst, Ti) * deltaT
                Ti += deltaT
            Next

            'result = this.IntegralSimpsonCp(T1, T2, 0.001, subst)

            result = result / this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Molar_Weight

            result2 = integral '/ this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Molar_Weight

            return result2*/
        }

        public double AUX_INT_CPDT_Ti(double T1, double T2, string subst )
        {
            /*
            Dim A, B, C, D, E, result As Double
            A = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_A
            B = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_B
            C = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_C
            D = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_D
            E = this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Ideal_Gas_Heat_Capacity_Const_E

            result = A * Log(T2 / T1) + B * (T2 - T1) + C / 2 * (T2 ^ 2 - T1 ^ 2) + D / 3 * (T2 ^ 3 - T1 ^ 3) + E / 4 * (T2 ^ 4 - T1 ^ 4)

            Dim deltaT As Double = (T2 - T1) / 10
            Dim result2, Ti As Double
            Dim i As Integer = 0
            Dim integral As Double = 0

            Ti = T1 + deltaT
            For i = 0 To 9
                integral += this.AUX_CPi(subst, Ti) * deltaT / (Ti - deltaT) '* Log(Ti / (Ti - deltaT))
                Ti += deltaT
            Next

            'result = this.IntegralSimpsonCp(T1, T2, 0.001, subst)

            result = result / this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Molar_Weight

            result2 = integral '/ this.CurrentMaterialStream.Fases(0).Componentes(subst).ConstantProperties.Molar_Weight

            return result2*/
        }

        public double AUX_INT_CPDTm(double T1, double T2, Fase fase)
        {
            /*
            Dim val As Double = 0
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

             switch fase

                case DTL.SimulationObjects.PropertyPackages.Fase.Mixture

                    For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                        If subst.FracaoMolar.GetValueOrDefault <> 0.0# And subst.FracaoMassica.GetValueOrDefault = 0.0# Then
                            subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, this.RET_PHASEID(fase))
                        End If
                        val += subst.FracaoMassica.GetValueOrDefault * this.AUX_INT_CPDTi(T1, T2, subst.Nome)
                    Next

                case DTL.SimulationObjects.PropertyPackages.Fase.Liquid

                    For Each subst In this.CurrentMaterialStream.Fases(1).Componentes.Values
                        If subst.FracaoMolar.GetValueOrDefault <> 0.0# And subst.FracaoMassica.GetValueOrDefault = 0.0# Then
                            subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, this.RET_PHASEID(fase))
                        End If
                        val += subst.FracaoMassica.GetValueOrDefault * this.AUX_INT_CPDTi(T1, T2, subst.Nome)
                    Next

                case DTL.SimulationObjects.PropertyPackages.Fase.Liquid1

                    For Each subst In this.CurrentMaterialStream.Fases(3).Componentes.Values
                        If subst.FracaoMolar.GetValueOrDefault <> 0.0# And subst.FracaoMassica.GetValueOrDefault = 0.0# Then
                            subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, this.RET_PHASEID(fase))
                        End If
                        val += subst.FracaoMassica.GetValueOrDefault * this.AUX_INT_CPDTi(T1, T2, subst.Nome)
                    Next

                case DTL.SimulationObjects.PropertyPackages.Fase.Liquid2

                    For Each subst In this.CurrentMaterialStream.Fases(4).Componentes.Values
                        If subst.FracaoMolar.GetValueOrDefault <> 0.0# And subst.FracaoMassica.GetValueOrDefault = 0.0# Then
                            subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, this.RET_PHASEID(fase))
                        End If
                        val += subst.FracaoMassica.GetValueOrDefault * this.AUX_INT_CPDTi(T1, T2, subst.Nome)
                    Next

                case DTL.SimulationObjects.PropertyPackages.Fase.Liquid3

                    For Each subst In this.CurrentMaterialStream.Fases(5).Componentes.Values
                        If subst.FracaoMolar.GetValueOrDefault <> 0.0# And subst.FracaoMassica.GetValueOrDefault = 0.0# Then
                            subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, this.RET_PHASEID(fase))
                        End If
                        val += subst.FracaoMassica.GetValueOrDefault * this.AUX_INT_CPDTi(T1, T2, subst.Nome)
                    Next

                case DTL.SimulationObjects.PropertyPackages.Fase.Vapor

                    For Each subst In this.CurrentMaterialStream.Fases(2).Componentes.Values
                        If subst.FracaoMolar.GetValueOrDefault <> 0.0# And subst.FracaoMassica.GetValueOrDefault = 0.0# Then
                            subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, this.RET_PHASEID(fase))
                        End If
                        val += subst.FracaoMassica.GetValueOrDefault * this.AUX_INT_CPDTi(T1, T2, subst.Nome)
                    Next

            End Select

            return val
            */
        }

        public double AUX_INT_CPDT_Tm(double T1, double T2, Fase fase)
        {
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                If subst.FracaoMolar.GetValueOrDefault <> 0.0# And subst.FracaoMassica.GetValueOrDefault = 0.0# Then
                    subst.FracaoMassica = this.AUX_CONVERT_MOL_TO_MASS(subst.Nome, this.RET_PHASEID(fase))
                End If
                val += subst.FracaoMassica.GetValueOrDefault * this.AUX_INT_CPDT_Ti(T1, T2, subst.Nome)
            Next

            return val*/
        }

        public double AUX_DELGF_T(double T1, double T2, string id,   bool mode2 = false) 
        {
            /*
            Dim dA As Double = 0
            Dim dB As Double = 0
            Dim dC As Double = 0
            Dim dD As Double = 0
            Dim dE As Double = 0
            Dim dHr As Double = 0
            Dim dGr As Double = 0
            Dim term3 As Double = 0

            Dim int1, int2 As Double
            Dim R = 8.314

            With this.CurrentMaterialStream.Fases(0).Componentes(id).ConstantProperties

                dHr = .IG_Enthalpy_of_Formation_25C
                dGr = .IG_Gibbs_Energy_of_Formation_25C

                If mode2 Then
                    If .IsIon Or .IsSalt Then
                        term3 = .Electrolyte_Cp0 * 1000 / .Molar_Weight * (Log(T2 / T1) + (T1 / T2) - 1) / R
                    Else
                        int1 = this.AUX_INT_CPDTi(T1, T2, id)
                        int2 = this.AUX_INT_CPDT_Ti(T1, T2, id)
                        term3 = int1 / (R * T2) - int2 / R
                    End If
                Else
                    int1 = this.AUX_INT_CPDTi(T1, T2, id)
                    int2 = this.AUX_INT_CPDT_Ti(T1, T2, id)
                    term3 = int1 / (R * T2) - int2 / R
                End If


            End With

            Dim result As Double

            If mode2 Then
                result = dGr / (R * T1) + dHr / R * (1 / T2 - 1 / T1) + term3
            Else
                result = (dGr - dHr) / (R * T1) + dHr / (R * T2) + term3
            End If

            return result*/
        }

        public double AUX_DELGig_RT(double T1, double T2, string[] ID, double[] stcoeff, int baseID ,   bool mode2 = false) 
        {
            /*
            Dim n As Integer = ID.Length

            Dim int1(n - 1), int2(n - 1), sint1, sint2, dgfT(n - 1), sumdgft As Double

            Dim dHr As Double = 0
            Dim dGr As Double = 0

            sint1 = 0
            sint2 = 0

            With this.CurrentMaterialStream

                Dim i As Integer = 0
                sumdgft = 0
                Do
                    dHr += stcoeff(i) * .Fases(0).Componentes(ID(i)).ConstantProperties.IG_Enthalpy_of_Formation_25C * .Fases(0).Componentes(ID(i)).ConstantProperties.Molar_Weight
                    dGr += stcoeff(i) * .Fases(0).Componentes(ID(i)).ConstantProperties.IG_Gibbs_Energy_of_Formation_25C * .Fases(0).Componentes(ID(i)).ConstantProperties.Molar_Weight
                    int1(i) = stcoeff(i) * this.AUX_INT_CPDTi(T1, T2, ID(i)) / stcoeff(baseID) * .Fases(0).Componentes(ID(i)).ConstantProperties.Molar_Weight
                    int2(i) = stcoeff(i) * this.AUX_INT_CPDT_Ti(T1, T2, ID(i)) / stcoeff(baseID) * .Fases(0).Componentes(ID(i)).ConstantProperties.Molar_Weight
                    sint1 += int1(i)
                    sint2 += int2(i)
                    dgfT(i) = stcoeff(i) * this.AUX_DELGF_T(T1, T2, ID(i), mode2) * .Fases(0).Componentes(ID(i)).ConstantProperties.Molar_Weight
                    sumdgft += dgfT(i) '/ stcoeff(baseID)
                    i = i + 1
                Loop Until i = n

            End With
            dHr /= Abs(stcoeff(baseID))
            dGr /= Abs(stcoeff(baseID))

            Dim R = 8.314

            Dim result = (dGr - dHr) / (R * T1) + dHr / (R * T2) + sint1 / (R * T2) - sint2 / R

            return sumdgft
            */
        }

        public double AUX_DELHig_RT(double T1, double T2, string[] ID, double[] stcoeff, int baseID)
        {
            /*            
            Dim n As Integer = ID.Length

            Dim int1(n - 1), sint1 As Double

            Dim dHr As Double = 0

            sint1 = 0

            With this.CurrentMaterialStream

                Dim i As Integer = 0
                Do
                    dHr += stcoeff(i) * .Fases(0).Componentes(ID(i)).ConstantProperties.IG_Enthalpy_of_Formation_25C * .Fases(0).Componentes(ID(i)).ConstantProperties.Molar_Weight
                    int1(i) = stcoeff(i) * this.AUX_INT_CPDTi(T1, T2, ID(i)) / Abs(stcoeff(baseID)) * .Fases(0).Componentes(ID(i)).ConstantProperties.Molar_Weight
                    sint1 += int1(i)
                    i = i + 1
                Loop Until i = n

            End With
            dHr /= Abs(stcoeff(baseID))

            Dim result = dHr + sint1

            return result / (8.314 * T2)
            */
        }   

        public double AUX_CONVERT_MOL_TO_MASS(string subst , int phasenumber ) 
        {
            /*
            Dim mol_x_mm As Double
            Dim sub1 As DTL.ClassesBasicasTermodinamica.Substancia
            For Each sub1 In this.CurrentMaterialStream.Fases(phasenumber).Componentes.Values
                mol_x_mm += sub1.FracaoMolar.GetValueOrDefault * sub1.ConstantProperties.Molar_Weight
            Next

            sub1 = this.CurrentMaterialStream.Fases(phasenumber).Componentes(subst)
            If mol_x_mm <> 0.0# Then
                return sub1.FracaoMolar.GetValueOrDefault * sub1.ConstantProperties.Molar_Weight / mol_x_mm
            Else
                return 0.0#
            End If*/
        }

        public double AUX_CONVERT_MASS_TO_MOL(string subst , int  phasenumber) 
        {
            /*
            Dim mass_div_mm As Double
            Dim sub1 As DTL.ClassesBasicasTermodinamica.Substancia
            For Each sub1 In this.CurrentMaterialStream.Fases(phasenumber).Componentes.Values
                mass_div_mm += sub1.FracaoMassica.GetValueOrDefault / sub1.ConstantProperties.Molar_Weight
            Next

            sub1 = this.CurrentMaterialStream.Fases(phasenumber).Componentes(subst)
            return sub1.FracaoMassica.GetValueOrDefault / sub1.ConstantProperties.Molar_Weight / mass_div_mm
            */
        }

        public virtual double DW_CalcSolidEnthalpy(double T, double[] Vx , List<ConstantProperties> cprops)
        {
            /*
            Dim n As Integer = UBound(Vx)
            Dim i As Integer
            Dim HS As Double = 0.0#
            Dim Cpi As Double

            For i = 0 To n
                If cprops(i).OriginalDB = "ChemSep" Or cprops(i).OriginalDB = "User" Then
                    Dim A, B, C, D, E, result As Double
                    Dim eqno As String = cprops(i).SolidHeatCapacityEquation
                    Dim mw As Double = cprops(i).Molar_Weight
                    A = cprops(i).Solid_Heat_Capacity_Const_A
                    B = cprops(i).Solid_Heat_Capacity_Const_B
                    C = cprops(i).Solid_Heat_Capacity_Const_C
                    D = cprops(i).Solid_Heat_Capacity_Const_D
                    E = cprops(i).Solid_Heat_Capacity_Const_E
                    '<SolidHeatCapacityCp name="Solid heat capacity"  units="J/kmol/K" >
                    result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) / 1000 / mw 'kJ/kg.K
                    Cpi = result
                Else
                    Cpi = 0.0#
                End If
                HS += Vx(i) * Cpi * (T - 298)
            Next

            return HS 'kJ/kg
            */
        }

        public virtual double DW_CalcSolidHeatCapacityCp(double T, double[] Vx , List<ConstantProperties> cprops )
        {
            /*
            Dim n As Integer = UBound(Vx)
            Dim i As Integer
            Dim Cp As Double = 0.0#
            Dim Cpi As Double

            For i = 0 To n
                If cprops(i).OriginalDB = "ChemSep" Or cprops(i).OriginalDB = "User" Then
                    Dim A, B, C, D, E, result As Double
                    Dim eqno As String = cprops(i).SolidHeatCapacityEquation
                    Dim mw As Double = cprops(i).Molar_Weight
                    A = cprops(i).Solid_Heat_Capacity_Const_A
                    B = cprops(i).Solid_Heat_Capacity_Const_B
                    C = cprops(i).Solid_Heat_Capacity_Const_C
                    D = cprops(i).Solid_Heat_Capacity_Const_D
                    E = cprops(i).Solid_Heat_Capacity_Const_E
                    '<SolidHeatCapacityCp name="Solid heat capacity"  units="J/kmol/K" >
                    result = this.CalcCSTDepProp(eqno, A, B, C, D, E, T, 0) / 1000 / mw 'kJ/kg.K
                    Cpi = result
                Else
                    Cpi = 0.0#
                End If
                Cp += Vx(i) * Cpi
            Next

            return Cp 'kJ/kg.K*/
        }

        public double[] RET_VMOL(Fase fase) 
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                val(i) = subst.FracaoMolar.GetValueOrDefault
                i += 1
            Next

            return val*/
        }

        public double RET_VMM()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.Molar_Weight
                i += 1
            Next

            return val*/
        }

        public double[] RET_VMAS(Fase fase) 
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0
            Dim sum As Double = 0.0#

            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                val(i) = subst.FracaoMassica.GetValueOrDefault
                sum += val(i)
                i += 1
            Next

            If sum <> 0.0# Then
                return val
            Else
                return AUX_CONVERT_MOL_TO_MASS(RET_VMOL(fase))
            End If*/
        }

        public double RET_VTC()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.Critical_Temperature
                i += 1
            Next

            return val*/
        }

        public double RET_VTF()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.TemperatureOfFusion
                i += 1
            Next

            return val*/
        }

        public double RET_VTB()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.Normal_Boiling_Point
                i += 1
            Next

            return val
            */
        }

        public double RET_VPC()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0


            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.Critical_Pressure
                i += 1
            Next

            return val
            */
          }

        public double[] RET_VZC()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.Critical_Compressibility
                i += 1
            Next

            return val*/
        }

        public double[] RET_VZRa()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.Z_Rackett
                i += 1
            Next
            return val*/
        }

        public double[] RET_VVC()
        {
            /*
            Dim vc, val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                vc = subst.ConstantProperties.Critical_Volume
                If subst.ConstantProperties.Critical_Volume = 0.0# Then
                    vc = m_props.Vc(subst.ConstantProperties.Critical_Temperature, subst.ConstantProperties.Critical_Pressure, subst.ConstantProperties.Acentric_Factor)
                End If
                val(i) = vc
                i += 1
            Next

            return val*/
        }

        public double[] RET_VW()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.Acentric_Factor
                i += 1
            Next

            return val*/
        }

        public double[] RET_VCP(double T)
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = this.AUX_CPi(subst.Nome, T)
                i += 1
            Next

            return val*/
        }

        public double[] RET_VHVAP(double T) 
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = this.AUX_HVAPi(subst.Nome, T)
                i += 1
            Next

            return val
            */
        }

        public double RET_Hid(double T1, double T2, Fase fase) 
        {
            /*
            Dim val As Double
            'Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            Dim phaseID As Integer

            If fase = PropertyPackages.Fase.Liquid Then phaseID = 1
            If fase = PropertyPackages.Fase.Liquid1 Then phaseID = 3
            If fase = PropertyPackages.Fase.Liquid2 Then phaseID = 4
            If fase = PropertyPackages.Fase.Liquid3 Then phaseID = 5
            If fase = PropertyPackages.Fase.Vapor Then phaseID = 2
            If fase = PropertyPackages.Fase.Mixture Then phaseID = 0

            'For Each subst In this.CurrentMaterialStream.Fases(phaseID).Componentes.Values
            '    val += subst.FracaoMassica.GetValueOrDefault * subst.ConstantProperties.Enthalpy_of_Formation_25C
            'Next

            return this.AUX_INT_CPDTm(T1, T2, fase) + val*/
        }

        public double RET_Hid(double T1, double T2, object Vz ) 
        {
            /*
            Dim val As Double
            'Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            Dim i As Integer = 0

            'For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
            '    val += this.AUX_CONVERT_MOL_TO_MASS(Vz)(i) * subst.ConstantProperties.Enthalpy_of_Formation_25C
            '    i = i + 1
            'Next

            return this.AUX_INT_CPDTm(T1, T2, this.AUX_CONVERT_MOL_TO_MASS(Vz)) + val*/
        }

        ///
        public double RET_Hid_i(double T1, double T2, string id)
        {
            return AUX_INT_CPDTi(T1, T2, id);
        }

        public double RET_Sid(double T1, double T2, double P2, Fase fase) 
        {
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            Dim phaseID As Integer

            If fase = PropertyPackages.Fase.Liquid Then phaseID = 1
            If fase = PropertyPackages.Fase.Liquid1 Then phaseID = 3
            If fase = PropertyPackages.Fase.Liquid2 Then phaseID = 4
            If fase = PropertyPackages.Fase.Liquid3 Then phaseID = 5
            If fase = PropertyPackages.Fase.Vapor Then phaseID = 2
            If fase = PropertyPackages.Fase.Mixture Then phaseID = 0
            'subst.FracaoMassica.GetValueOrDefault * subst.ConstantProperties.Standard_Absolute_Entropy 
            For Each subst In this.CurrentMaterialStream.Fases(phaseID).Componentes.Values
                If subst.FracaoMolar.GetValueOrDefault <> 0 Then val += -8.314 * subst.FracaoMolar.GetValueOrDefault * Math.Log(subst.FracaoMolar.GetValueOrDefault) / subst.ConstantProperties.Molar_Weight
            Next

            Dim tmp = this.AUX_INT_CPDT_Tm(T1, T2, fase) + val - 8.314 * Math.Log(P2 / 101325) / this.AUX_MMM(fase)

            return tmp*/
        }

        public double RET_Sid(double T1, double T2, double P2, object Vz) 
        {
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim Vw = this.AUX_CONVERT_MOL_TO_MASS(Vz)

            Dim i As Integer = 0

            'Vw(i) * subst.ConstantProperties.Standard_Absolute_Entropy
            val = 0
            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                If Vz(i) <> 0 Then val += -8.314 * Vz(i) * Math.Log(Vz(i)) / subst.ConstantProperties.Molar_Weight
                i = i + 1
            Next
            Dim tmp1 = 8.314 * Math.Log(P2 / 101325) / this.AUX_MMM(Vz)
            Dim tmp2 = this.AUX_INT_CPDT_Tm(T1, T2, this.AUX_CONVERT_MOL_TO_MASS(Vz))
            return tmp2 + val - tmp1*/
        }

        public double RET_Sid_i(double T1, double T2, double P2, string id) 
        {
            /*
            Dim val As Double

            Dim tmp1 = 8.314 * Math.Log(P2 / 101325) / this.CurrentMaterialStream.Fases(0).Componentes(id).ConstantProperties.Molar_Weight
            Dim tmp2 = this.AUX_INT_CPDT_Ti(T1, T2, id)
            return tmp2 + val - tmp1*/
        }

        public double RET_Gid(double T1, double T2, double P2, object Vz )
        {    
            /*
            Dim hid = this.RET_Hid(T1, T2, Vz)
            Dim sid = this.RET_Sid(T1, T2, P2, Vz)

            return hid - T2 * sid*/
        }

        public double RET_Gid_i(double T1, double T2, double P2, string id) 
        {
            /*
            Dim hid = this.RET_Hid_i(T1, T2, id)
            Dim sid = this.RET_Sid_i(T1, T2, P2, id)

            return hid - T2 * sid*/
        }

        public double RET_Gid(double T1, double T2, double P2, Fase fase)
        { 
            /*
            Dim hid = this.RET_Hid(T1, T2, fase)
            Dim sid = this.RET_Sid(T1, T2, P2, fase)

            return hid - T2 * sid*/
        }

        public virtual double[,] RET_VKij()     
        {
            /*
            If this.ParametrosDeInteracao Is Nothing Then
                this.ParametrosDeInteracao = New DataTable
                With this.ParametrosDeInteracao.Columns
                    For Each subst As DTL.ClassesBasicasTermodinamica.Substancia In this.CurrentMaterialStream.Fases(0).Componentes.Values
                        .Add(subst.ConstantProperties.ID, GetType(System.Double))
                    Next
                End With

                With this.ParametrosDeInteracao.Rows
                    For Each subst As DTL.ClassesBasicasTermodinamica.Substancia In this.CurrentMaterialStream.Fases(0).Componentes.Values
                        .Add()
                    Next
                End With
            End If

            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1, this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim i As Integer = 0
            Dim l As Integer = 0
            For Each r As DataRow In this.ParametrosDeInteracao.Rows
                i = 0
                Do
                    If l <> i And r.Item(i).GetType().ToString <> "System.DBNull" Then
                        Dim value As Double
                        If Double.TryParse(r.Item(i), value) Then
                            val(l, i) = r.Item(i)
                        Else
                            val(l, i) = 0
                        End If
                    Else
                        val(l, i) = 0
                    End If
                    i = i + 1
                Loop Until i = this.ParametrosDeInteracao.Rows.Count
                l = l + 1
            Next

            return val*/
        }

        public double[] RET_VCSACIDS()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As String
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.COSMODBName
                If val(i) = "" Then val(i) = subst.ConstantProperties.CAS_Number
                i += 1
            Next

            return val*/
        }

        public void RET_VIDS()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As String
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.ID
                i += 1
            Next

            return val*/
        }

        public void RET_VCAS()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As String
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.CAS_Number
                i += 1
            Next

            return val*/
        }

        public void RET_VNAMES()
        {
            /*
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As String
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = subst.ConstantProperties.Name
                i += 1
            Next

            return val*/
        }

        public void RET_NullVector()
        {
            /*    
            Dim val(this.CurrentMaterialStream.Fases(0).Componentes.Count - 1) As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            Dim i As Integer = 0
            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val(i) = 0.0#
                i += 1
            Next
            return val*/
        }

        public Fase RET_PHASECODE(int phaseID ) 
        {
            /*
             switch phaseID
                case 0
                    return Fase.Mixture
                case 1
                    return Fase.Liquid
                case 2
                    return Fase.Vapor
                case 3
                    return Fase.Liquid1
                case 4
                    return Fase.Liquid2
                case 5
                    return Fase.Liquid3
                case 6
                    return Fase.Aqueous
                case 7
                    return Fase.Solid
            End Select*/
        }

        public internal  RET_PHASEID(Fase phasecode ) 
        {
            /*
             switch phasecode
                case Fase.Mixture
                    return 0
                case Fase.Liquid
                    return 1
                case Fase.Vapor
                    return 2
                case Fase.Liquid1
                    return 3
                case Fase.Liquid2
                    return 4
                case Fase.Liquid3
                    return 5
                case Fase.Aqueous
                    return 6
                case Fase.Solid
                    return 7
                case Else
                    return -1
            End Select*/
        }

        public double AUX_MMM(object Vz) 
        {
            /*
            Dim val As Double
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            Dim i As Integer = 0

            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val += Vz(i) * subst.ConstantProperties.Molar_Weight
                i += 1
            Next

            return val*/
        }

        public double[] AUX_NORMALIZE(object Vx ) 
        {
            /*
            Dim sum As Double = 0
            Dim i, n As Integer

            n = UBound(Vx)

            Dim Vxnew(n) As Double

            For i = 0 To n
                sum += Vx(i)
            Next

            For i = 0 To n
                Vxnew(i) = Vx(i) / sum
            Next

            return Vxnew*/
        }

        public double[] AUX_ERASE(object Vx )
        {
            /*
            Dim i, n As Integer

            n = UBound(Vx)

            Dim Vx2(n) As Double

            For i = 0 To n
                Vx2(i) = 0
            Next

            return Vx2*/
         }

        public int AUX_SINGLECOMPIDX(object Vx )
        {
            /*
            Dim i, n As Integer

            n = UBound(Vx)

            For i = 0 To n
                If Vx(i) <> 0 Then return i
            Next

            return -1*/
        }

        public int AUX_SINGLECOMPIDX(Fase fase) 
        {
            /*
            Dim i As Integer

            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            i = 0
            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                If subst.FracaoMolar.GetValueOrDefault <> 0 Then return i
            Next

            return -1*/
        }

        public bool AUX_IS_SINGLECOMP(object Vx )
        {
            /*
            Dim i, c, n As Integer

            n = UBound(Vx)

            c = 0
            For i = 0 To n
                If Vx(i) <> 0 Then c += 1
            Next

            If c = 1 Then return True Else return False*/
        }

        public bool AUX_IS_SINGLECOMP(Fase fase)
        {
            /*
            Dim c As Integer

            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia

            c = 0
            For Each subst In this.CurrentMaterialStream.Fases(this.RET_PHASEID(fase)).Componentes.Values
                If subst.FracaoMolar.GetValueOrDefault <> 0 Then c += 1
            Next

            If c = 1 Then return True Else return False*/
         }


        public double AUX_INT_CPDTm(double T1, double T2, object Vw )
        {
            /*
            Dim val As Double
            Dim i As Integer = 0
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val += Vw(i) * this.AUX_INT_CPDTi(T1, T2, subst.Nome)
                i += 1
            Next

            return val*/
        }

        public  AUX_INT_CPDT_Tm(double T1, double T2, object Vw )
        {
            /*
            Dim val As Double
            Dim i As Integer = 0
            Dim subst As DTL.ClassesBasicasTermodinamica.Substancia
            For Each subst In this.CurrentMaterialStream.Fases(0).Componentes.Values
                val += Vw(i) * this.AUX_INT_CPDT_Ti(T1, T2, subst.Nome)
                i += 1
            Next

            return val
            */
        }

        public double[] AUX_CONVERT_MOL_TO_MASS(object Vz ) 
        {
            /*
            Dim Vwe(UBound(Vz)) As Double
            Dim mol_x_mm As Double = 0
            Dim i As Integer = 0
            Dim sub1 As DTL.ClassesBasicasTermodinamica.Substancia
            For Each sub1 In this.CurrentMaterialStream.Fases(0).Componentes.Values
                mol_x_mm += Vz(i) * sub1.ConstantProperties.Molar_Weight
                i += 1
            Next

            i = 0
            For Each sub1 In this.CurrentMaterialStream.Fases(0).Componentes.Values
                If mol_x_mm <> 0 Then
                    Vwe(i) = Vz(i) * sub1.ConstantProperties.Molar_Weight / mol_x_mm
                Else
                    Vwe(i) = 0.0#
                End If
                i += 1
            Next

            return Vwe*/
        }

        public double[]  AUX_CONVERT_MASS_TO_MOL(object Vz )
        {
            /*
            Dim Vw(UBound(Vz)) As Double
            Dim mass_div_mm As Double
            Dim i As Integer = 0
            Dim sub1 As DTL.ClassesBasicasTermodinamica.Substancia
            For Each sub1 In this.CurrentMaterialStream.Fases(0).Componentes.Values
                mass_div_mm += Vz(i) / sub1.ConstantProperties.Molar_Weight
                i += 1
            Next

            i = 0
            For Each sub1 In this.CurrentMaterialStream.Fases(0).Componentes.Values
                Vw(i) = Vz(i) / sub1.ConstantProperties.Molar_Weight / mass_div_mm
                i += 1
            Next

            return Vw*/
        }

        public double AUX_CalculateSumSquares(object Vz ) 
        {
            /*
            Dim n, i As Integer
            n = UBound(Vz)
            Dim sum As Double = 0.0#

            For i = 0 To n
                sum += Vz(i) ^ 2
            Next

            return sum*/
        }

        public double AUX_CalculateAbsSumSquares(object Vz )
        {   
            /*
            Dim n, i As Integer
            n = UBound(Vz)
            Dim sum As Double = 0.0#

            For i = 0 To n
                sum += Abs(Vz(i)) ^ 2
            Next

            return sum*/
        }

        public double AUX_CalculateSum(object Vz )
        {
            /*
            Dim n, i As Integer
            n = UBound(Vz)
            Dim sum As Double = 0.0#

            For i = 0 To n
                sum += Vz(i)
            Next

            return sum*/
        }

        public double AUX_CalculateAbsSum(object Vz ) 
        {
            /*
            Dim n, i As Integer
            n = UBound(Vz)
            Dim sum As Double = 0.0#

            For i = 0 To n
                sum += Abs(Vz(i))
            Next

            return sum*/
        }    

        public bool AUX_CheckTrivial(object  KI)
        {
            /*
            Dim isTrivial As Boolean = True
            Dim n, i As Integer
            n = UBound(KI)

            For i = 0 To n
                If Abs(KI(i) - 1) > 0.0001 Then isTrivial = False
            Next

            return isTrivial*/
        }

        public double CalcCSTDepProp(string eqno , double  A , double B , double C , double D, double  E, double T,double Tc )
        {
            /*            
            Dim Tr As Double = T / Tc

             switch eqno
                case "1"
                    return A
                case "2"
                    return A + B * T
                case "3"
                    return A + B * T + C * T ^ 2
                case "4"
                    return A + B * T + C * T ^ 2 + D * T ^ 3
                case "5"
                    return A + B * T + C * T ^ 2 + D * T ^ 3 + E * T ^ 4
                case "10"
                    return Exp(A - B / (T + C))
                case "11"
                    return Exp(A)
                case "12"
                    return Exp(A + B * T)
                case "13"
                    return Exp(A + B * T + C * T ^ 2)
                case "14"
                    return Exp(A + B * T + C * T ^ 2 + D * T ^ 3)
                case "15"
                    return Exp(A + B * T + C * T ^ 2 + D * T ^ 3 + E * T ^ 4)
                case "16"
                    return A + Exp(B / T + C + D * T + E * T ^ 2)
                case "17"
                    return A + Exp(B + C * T + D * T ^ 2 + E * T ^ 3)
                case "45"
                    return A * T + B * T ^ 2 / 2 + C * T ^ 3 / 3 + D * T ^ 4 / 4 + E * T ^ 5 / 5
                case "75"
                    return B + 2 * C * T + 3 * D * T ^ 2 + 4 * E * T ^ 3
                case "100"
                    return A + B * T + C * T ^ 2 + D * T ^ 3 + E * T ^ 4
                case "101"
                    return Exp(A + B / T + C * Log(T) + D * T ^ E)
                case "102"
                    return A * T ^ B / (1 + C / T + D / T ^ 2)
                case "103"
                    return A + B * Exp(-C / (T ^ D))
                case "104"
                    return A + B / T + C / T ^ 2 + D / T ^ 8 + E / T ^ 9
                case "105"
                    return A / (B ^ (1 + (1 - T / C) ^ D))
                case "106"
                    return A * (1 - Tr) ^ (B + C * Tr + D * Tr ^ 2 + E * Tr ^ 3)
                case "107"
                    return A + B * (C / T / Sinh(C / T)) ^ 2 + D * (D / T / Cosh(D / T)) ^ 2
                case "114"
                    return A * T + B * T ^ 2 / 2 + C * T ^ 3 / 3 + D * T ^ 4 / 4
                case "115"
                    return Exp(A + B / T + C * Log(T) + D * T ^ 2 + E / T ^ 2)
                case "116"
                    return A + B * (1 - Tr) ^ 0.35 + C * (1 - Tr) ^ (2 / 3) + D * (1 - Tr) + E * (1 - Tr) ^ (4 / 3)
                case "117"
                    return A * T + B * (C / T) / Tanh(C / T) - D * (E / T) / Tanh(E / T)
                case "207"
                    return Exp(A - B / (T + C))
                case "208"
                    return 10 ^ (A - B / (T + C))
                case "209"
                    return 10 ^ (A * (1 / T - 1 / B))
                case "210"
                    return 10 ^ (A + B / T + C * T + D * T ^ 2)
                case "211"
                    return A * ((B - T) / (B - C)) ^ D
                case Else
                    return 0
            End Select
                */
        }

/*
#Region "   IDisposable Support "
        private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected virtual Sub Dispose(ByVal disposing As Boolean)
            If Not this.disposedValue Then
                If disposing Then
                    ' TODO: free other state (managed objects).
                End If

                ' TODO: free your own state (unmanaged objects).
                If _como IsNot Nothing Then
                    If System.Runtime.InteropServices.Marshal.IsComObject(_como) Then
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(_como)
                    Else
                        _como =null;
                    End If
                End If
                If _pme IsNot Nothing Then
                    If System.Runtime.InteropServices.Marshal.IsComObject(_pme) Then
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(_pme)
                    Else
                        _pme =null;
                    End If
                End If


                ' TODO: set large fields to null.
            End If
            this.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        public virtual Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

#End Region
*/
}
    /// <summary>
    /// Class to store Phase Info and mapping for CAPE-OPEN Property Packages
    /// </summary>
    /// <remarks>Used only in the context of CAPE-OPEN Objects.</remarks>
    [Serializable]
    public class PhaseInfo
    {
        public string PhaseLabel = "";
        public int DWPhaseIndex;
        public Fase DWPhaseID = Fase.Aqueous;

        public PhaseInfo (string pl , int pi , Fase pid)
        {
            PhaseLabel = pl,
            DWPhaseIndex = pi,
            DWPhaseID = pid,
        }
    }

    /*
    /// <summary>
    /// COM IStream Class Implementation
    /// </summary>
    /// <remarks></remarks>
    <System.Serializable()> public Class ComStreamWrapper
        Inherits System.IO.Stream
        private mSource As IStream
        private mInt64 As IntPtr

        public void New(ByVal source As IStream)
            mSource = source
            mInt64 = iop.Marshal.AllocCoTaskMem(8)
        End Sub

        Protected Overrides Sub Finalize()
            Try
                iop.Marshal.FreeCoTaskMem(mInt64)
            Finally
                MyBase.Finalize()
            End Try
        End Sub

        public Overrides ReadOnly Property CanRead() As Boolean
            Get
                return True
            End Get
        End Property

        public Overrides ReadOnly Property CanSeek() As Boolean
            Get
                return True
            End Get
        End Property

        public Overrides ReadOnly Property CanWrite() As Boolean
            Get
                return True
            End Get
        End Property

        public Overrides Sub Flush()
            mSource.Commit(0)
        End Sub

        public Overrides ReadOnly Property Length() As Long
            Get
                Dim stat As ComTypes.STATSTG
                stat =null;
                mSource.Stat(stat, 1)
                return stat.cbSize
            End Get
        End Property

        public Overrides Property Position() As Long
            Get
                Throw New NotImplementedException()
            End Get
            Set(ByVal value As Long)
                Throw New NotImplementedException()
            End Set
        End Property

        public Overrides Function Read(ByVal buffer As Byte(), ByVal offset As Integer, ByVal count As Integer) As Integer
            If offset <> 0 Then
                Throw New NotImplementedException()
            End If
            mSource.Read(buffer, count, mInt64)
            return iop.Marshal.ReadInt32(mInt64)
        End Function

        public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
            mSource.Seek(offset, CInt(origin), mInt64)
            return iop.Marshal.ReadInt64(mInt64)
        End Function

        public Overrides Sub SetLength(ByVal value As Long)
            mSource.SetSize(value)
        End Sub

        public Overrides Sub Write(ByVal buffer As Byte(), ByVal offset As Integer, ByVal count As Integer)
            If offset <> 0 Then
                Throw New NotImplementedException()
            End If
            mSource.Write(buffer, count, IntPtr.Zero)
        End Sub

    End Class*/
 }
